下面的代码对在Excel工作表中粘贴为(由其他宏)为msorectangle形状的图片进行计数,并将它们放置在每行之间特定距离的1行中。我需要在定位和编码方面陷入困境。问题是在以下情况下如何升级此代码:
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
答案 0 :(得分:2)
对于倒数第二个条件,如果总图片数量为12,那么我可以安全地假设每行需要6个图片。对于最后一个条件,您希望每行7个。对于这两个,我们将使用.CognitoEvent
.ConfigEvent
.DynamodbEvent
.KinesisEvent
.S3Event
.SNSEvent
,然后将为此目的执行Counter
或Counter Mod 6
。您可以阅读MS KB中的Mod operator。
逻辑是在最后2个条件的下一行重置Counter Mod 7
和.Top
。我们将为此使用布尔变量。
这是您要尝试的吗?
.Left
屏幕截图
答案 1 :(得分:1)
- 如果图片数量为1列多于6张,并且将尺寸设置为h:7.25cm w:4.7厘米
- 如果图片数量> 7并且<= 10,则显示1行尺寸为h:5,9cm w:3,8cm的图片
- 如果图片数小于12,则小于2行,其大小从1点开始。
- 如果图片数量> 12,则每7张图片从下一行开始,其大小从nr 2点开始
因此,如果我们以i
作为图片数量:
我们可以做一些简单的计算来检查满足哪个条件,并使用Select Case
识别并分配您的4种情况,如下所示:
Select Case i
Case IS >= 12
numberofrows = i \ 7 '(this only gives whole numbers)
Formatting = 2
Case IS > 10
numberofrows = 2
Formatting = 1
Case IS >= 7
numberofrows = 1
Formatting = 2
Case ELSE
numberofrows = 1
Formatting = 1
End Select