下面的代码计数将(由其他宏)粘贴为 excel 工作表中的 msorectangle 形状的图片,并将它们放置在每行之间特定距离的 1 行中。我需要为定位添加另一个限制,并且我正在努力编码它。问题是在以下情况下如何升级此代码:
- 如果图片数量 <=6 多于 1 行图片并将尺寸设置为 h:7,25cm w:4,7cm
- 如果图片数量 >6 且 <=11,则 1 行图片尺寸为 h:5,9cm w:3,8cm
- 如果图片数量 = 12 比 2 行,尺寸从 1 点开始 h:7,25cm w:4,7cm。
- 如果图片数量大于 12,则每张图片(7、13、19、25 等图片)从下一行开始,尺寸从点 nr 2 h:5,9cm w:3,8cm
图片列表是动态的。
Sub Sample2()
Dim shp As Shape, shp2 As Shape
Dim ws As Worksheet
Dim lstShp As Integer
Dim shpLft As Double, shpTop As Double, shpWidth As Double, shpHeight As Double
Dim inBetweenMargin As Double
Dim i As Long
'~~> In betwen margin
inBetweenMargin = 8
Set ws = ThisWorkbook.Worksheets("wk")
With ws
'~~> Get the max shape number(name)
For Each shp In .Shapes
If shp.AutoShapeType = msoShapeRectangle Then
If Val(shp.Name) > 1 And Val(shp.Name) > lstShp Then _
lstShp = Val(shp.Name)
End If
Next
'~~> Loop through the shapes
For i = 1 To lstShp
'~~> This is required in case you delete shape 3
'~~> and have only shapes 1,2,4,5 etc...
On Error Resume Next
Set shp = .Shapes(CStr(i))
'shp2 = first photo
Set shp2 = ws.Shapes("1")
On Error GoTo 0
'~~> position them
If Not shp Is Nothing And shp.AutoShapeType = msoShapeRectangle Then
If shpLft = 0 And shpTop = 0 And shpWidth = 0 Then
shpLft = shp.Left
shpTop = shp.Top
shpWidth = shp.Width
Else
shp.Top = shpTop
shp.Left = shpLft + shpWidth + inBetweenMargin
shpLft = shp.Left
shpWidth = shp.Width
End If
End If
'position picture nr 7 and above in second row
If Val(shp.Name) = 7 Then
shp.Top = shp2.Top + shp2.Height + inBetweenMargin
shp.Left = shp2.Left
shpLft = shp.Left
shpWidth = shp.Width
End If
If Val(shp.Name) >= 8 Then
shp.Top = shp2.Top + shp2.Height + inBetweenMargin
End If
Next i
End With
End Sub