2017-03-06 46 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