Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:需要对形状进行排序_Vba_Excel - Fatal编程技术网

VBA:需要对形状进行排序

VBA:需要对形状进行排序,vba,excel,Vba,Excel,最近,在一次采访中,我在VBA中遇到了一个问题。问题是: 编写一个程序对工作表中的形状进行排序,例如:我有各种形状,如圆形、三角形、矩形、五边形。。。需要对其进行排序,并将其一个置于另一个之下。 我尝试了Shapes对象和msoshapeRectangle方法。但它不起作用 你能告诉我这有可能吗 谢谢这是一个有趣的挑战,所以我做到了。不妨发布结果(为了清晰起见,请发表评论): Worksheet.Shapes集合公开Worksheet中的所有Shape对象,Shape对象公开它的TopLeftC

最近,在一次采访中,我在VBA中遇到了一个问题。问题是:

编写一个程序对工作表中的形状进行排序,例如:我有各种形状,如圆形、三角形、矩形、五边形。。。需要对其进行排序,并将其一个置于另一个之下。 我尝试了Shapes对象和msoshapeRectangle方法。但它不起作用

你能告诉我这有可能吗


谢谢

这是一个有趣的挑战,所以我做到了。不妨发布结果(为了清晰起见,请发表评论):


Worksheet.Shapes
集合公开
Worksheet
中的所有
Shape
对象,
Shape
对象公开它的
TopLeftCell
BottomRightCell
。使用此方法,可以很容易地将所有形状排列在一列中。您可以通过引用其
AutoShapeType
属性来判断形状对象是什么类型的形状。什么样的面试可能会要求某人编写如此复杂(且有些无用的程序)?很好的工作顺便说一句,测试和工作良好@讽刺的是,我同意,这很奇怪。我认为问题的关键在于迫使程序员跳出框框思考,并适应他们以前可能从未遇到过的挑战。这可能是一个很好的方法,可以剔除那些根本无法编写代码的人,或者在投入足够的精力至少尝试找到解决方案之前放弃的人。非常正确。我给它打了一针,但打坏了。。。在我的excel工作中,我很少使用形状,所以我对任何术语/类型都不熟悉,否则我就不会有争议了!如果他们想看看寻找解决方案的速度和简单性。录制宏并将形状拖动到所需位置。这将在1分钟内解决。问题是如何收到它haha@tigeravatar... 谢谢你的精彩代码。。。我通过在Excel中插入形状来尝试此代码。。。工作正常。。然而,它将矩形置于三角形之前。。。我不确定我是否做错了什么。。。
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