最近、インタビューでVBAで質問が発生しました。質問は:VBA:シェイプをソートする必要があります
私は円、三角、矩形、五角形のような様々な図形を持っている、ワークシートの図形を並べ替えるためのプログラムを書いてください。これはソートされ、上下に配置する必要があります。 ShapesオブジェクトとmsoshapeRectangleメソッドを試しました。しかし、それは仕事をしなかった。
これは可能でしょうか教えてください。
おかげ
最近、インタビューでVBAで質問が発生しました。質問は:VBA:シェイプをソートする必要があります
私は円、三角、矩形、五角形のような様々な図形を持っている、ワークシートの図形を並べ替えるためのプログラムを書いてください。これはソートされ、上下に配置する必要があります。 ShapesオブジェクトとmsoshapeRectangleメソッドを試しました。しかし、それは仕事をしなかった。
これは可能でしょうか教えてください。
おかげ
それは興味深い挑戦だったので、私はそれをやりました。同様に(明確にするため、コメント)の結果を投稿可能性があります:
Sub tgr()
'There are 184 total AutoShapeTypes
'See here for full list
'https://msdn.microsoft.com/VBA/Office-Shared-VBA/articles/msoautoshapetype-enumeration-office
Dim aShapeTypes(1 To 184) As String
Dim ws As Worksheet
Dim Shp As Shape
Dim i As Long, j As Long
Dim vShpName As Variant
Dim dLeftAlign As Double
Dim dTopAlign As Double
Dim dVerticalInterval As Double
Dim dHorizontalInterval As Double
Dim dPadding As Double
Set ws = ActiveWorkbook.ActiveSheet
'Sort order will be by the AutoShapeType numerical ID
'Using this, shapes will be sorted in this order (incomplete list for brevity):
' Rectangle, Parallelogram, Trapezoid, Diamond, Rounded rectangle, Octagon, Isosceles triangle, Right triangle, Oval, Hexagon
'Note that you can use a Select Case to order shapes to a more customized list
'I use this method to put the -2 (indicates a combination of the other states) at the bottom of the sort order
For Each Shp In ws.Shapes
Select Case Shp.AutoShapeType
Case -2: aShapeTypes(UBound(aShapeTypes)) = aShapeTypes(UBound(aShapeTypes)) & "||" & Shp.Name
Case Else: aShapeTypes(Shp.AutoShapeType) = aShapeTypes(Shp.AutoShapeType) & "||" & Shp.Name
End Select
Next Shp
'Now that all shapes have been collected and put into their sort order, perform the actual sort operation
'Adjust the alignment and vertical veriables as desired
'The Padding variable is so that the shapes don't start at the very edge of the sheet (can bet set to 0 if that's fine)
'I have it currently set to sort the shapes vertically, but they can be sorted horizontally by uncommenting those lines and commenting out the vertical sort lines
dPadding = 10
dLeftAlign = 5
dTopAlign = 5
dVerticalInterval = 40
dHorizontalInterval = 40
j = 0
For i = LBound(aShapeTypes) To UBound(aShapeTypes)
If Len(aShapeTypes(i)) > 0 Then
For Each vShpName In Split(Mid(aShapeTypes(i), 3), "||")
With ws.Shapes(vShpName)
'Vertical Sort
.Left = dLeftAlign
.Top = j * dVerticalInterval + dPadding
'Horizont Sort
'.Top = dTopAlign
'.Left = j * dHorizontalInterval + dPadding
End With
j = j + 1
Next vShpName
End If
Next i
End Sub
どのようなインタビューで、誰かがそのような複雑な(そしてやや役に立たないプログラム)を書くことを求めている可能性がありますか?いい仕事btw、テストされ、それはうまく動作します! – dwirony
@dwirony私は同意する、それはかなり変だ。私は質問のポイントは、プログラマーにボックスの外で考えさせ、かつて決してしなかったかもしれない挑戦に適応させることです。少なくともコードを書くことができない人や、あまりにも解決策を見つけようと考えてしまう前にあきらめる人を排除するための良い方法でしょう。 – tigeravatar
非常に真実。私はそれを打ったが、タップアウト...私は非常にほとんどのExcelワークの形で動作しないので、私は任意の用語/タイプに精通していない、私は競合していただろう! – dwirony
'Worksheet.Shapes'コレクションは' Worksheet'以内 'Shape'オブジェクトのすべてを公開すると' Shape'オブジェクトは、それは 'TopLeftCell'だ露光し、 'BottomRightCell'。これを使用すると、すべての図形を列に配置するのは非常に簡単です。 –
'AutoShapeType'プロパティを参照することで、Shapeオブジェクトの形状の種類を知ることができます。 – tigeravatar