2017-03-06 66 views
0

我目前有一大堆工作簿,每个工作簿都包含一张带有数百个形状的工作表,我需要一个代码来选择其最宽的形状并获取其宽度的值。 谢谢通过形状循环来获得Dimensions

Sub GetWidestShape() 
     for each shape in activesheet.shapes 
     ' if shape is widest then 
     ' shape.copy 
     ' range("a1").value=the width of the widest shape 
    end sub 
+0

欢迎来到Stackoverflow!在发布问题时,请分享您的代码,并尝试尽可能好地解释您的问题。现在,你所要求的东西很不清楚。 –

+0

这个问题的确切部分是你有麻烦吗? –

+0

嗨,再次。对不起,在问题中不太清楚。这里是一段代码,以更好地解释 – user7381321

回答

1

你可以使用一个函数返回最宽的形状,并设置其宽度也

Function GetWidestShape(widestShpWidth As Long) As Shape 
    Dim shp As Shape 

    For Each shp In ActiveSheet.Shapes 
     If shp.Width > widestShpWidth Then 
      widestShpWidth = shp.Width 
      Set GetWidestShape = shp 
     End If 
    Next 
End Function 

,你可以在你的主代码漏洞如下:

Sub Main() 
    Dim widestShp As Shape 
    Dim widestShpWidth As Long 

    Set widestShp = GetWidestShape(widestShpWidth) '<--| get the widest shape along with ist width 
    With widestShp 
     ' ... 
     ' your code to act on referenced shape 
     '... 
    End With 
End Sub 

当然的做法也是可以的:

Function GetWidestShapeWidth(widestShp As Shape) As Long 
    Dim shp As Shape 
    Dim widestShpWidth As Long 

    For Each shp In ActiveSheet.Shapes 
     If shp.Width > widestShpWidth Then 
      widestShpWidth = shp.Width 
      Set widestShp = shp 
     End If 
    Next 
End Function 


Sub Main() 
    Dim widestShp As Shape 
    Dim widestShpWidth As Long 

    widestShpWidth = GetWidestShapeWidth(widestShp) '<--| get the width of the widest shape along with the widest shape   
    With widestShp 
     ' ... 
     ' your code to act on referenced shape 
     '... 
    End With 
End Sub 
+0

@ user7381321,如果我的答案解决了您的问题,请点击答案旁边的复选标记以接受它,将其从灰色切换到填充。谢谢 – user3598756

0

在猜测它会是这样的....注意我没有获得现在来测试这个权利。

Sub GetWidestShape() 
     dim widest 
     dim i as int; 
     i=0 

     for each shape in activesheet.shapes 
      if(shape.width > widest.width or i =0) then 
       widest = shape 
      end if 
      i++ 
     next 
     'Do whatever you want with the shape. 
     'you should be able to refrence the shape with the variable/object name "widest" 
     'E.g range("a1").value=widest.width 
end sub