如何确定包含各种形状的工作表上的形状类型?

时间:2014-11-23 17:26:36

标签: excel vba excel-vba

我想循环浏览工作表上的形状以确定它们是否为DropDown,然后是否特定下拉列表位于特定行上。该工作表包含许多复选框和下拉列表。 我正在使用循环创建下拉列表,如下所示:

 wsBank.DropDowns.Add(Range(AnsPositionAddress).Left, Range(AnsPositionAddress).Top, Range(AnsPositionAddress).Width, Range(AnsPositionAddress).Height).Select
With Selection
    '.ListFillRange = AnsPositionRng.Offset(0, 1)
    '.LinkedCell = AnsPositionRng.Offset(0, 1)
    .DropDownLines = 4
    .Display3DShading = False
    .Name = "QuestionDrop" & QuizQuestionNumber
    .OnAction = "recordAnswer"
End With

Number = 1
For Each Q In AnsRng
    wsBank.DropDowns("QuestionDrop" & QuizQuestionNumber).AddItem Number & " -  " & Q
    Number = Number + 1
Next Q

我正在创建如下复选框:

ActiveSheet.CheckBoxes.Add(Range(chkbxAddress).Left, Range(chkbxAddress).Top, Range(chkbxAddress).Width, Range(chkbxAddress).Height).Select
With Selection
    .Caption = ""
    .Value = xlOff
    .LinkedCell = AnsPositionRng.Offset(0, -4).Address
    .Display3DShading = True
End With

如果一个范围内的特定单元格包含'True',那么我想找到同一行上的下拉列表,所以我要遍历形状,确定形状是否为下拉列表,然后检查其BottomRight Cell属性以查看它是否与包含“True”的行匹配。 这可能吗?我到目前为止:

 wsBank.Activate
With wsBank
    IndicatorLstRow = .Range("C" & .Rows.Count).End(xlUp).Row
    Set IndicatorRng = wsBank.Range("B4:B" & IndicatorLstRow)
End With

For Each c In IndicatorRng
    If c = "True" Then
        QuestionRow = c.Row

            For Each ComboShape In wsBank.Shapes
               test = ComboShape.Type
            Next ComboShape

    End If
Next c

2 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。首先找到包含" True"的单元格。这将为您提供单元格地址,然后您将获得单元格行。接下来,只需遍历各个形状并找到它们的.TopLeftCell,然后从中获取行并查看它是否匹配。

以下是一个示例( UNTESTED

Dim shp As Shape

For Each c In IndicatorRng
    If c = "True" Then
        QuestionRow = c.Row
        For Each shp In wsBank.Shapes
            If shp.Type = 8 And shp.Name Like "Drop*" Then
                If shp.TopLeftCell.Row = QuestionRow Then
                    '
                    '~~> Rest of the code
                    '
                End If
            End If
        Next shp
    End If
Next c

答案 1 :(得分:1)

如果你跑:

Sub IdentifyShapes()
    Dim s As Shape
    For Each s In ActiveSheet.Shapes
        MsgBox s.Type & vbCrLf & s.Name
    Next s
End Sub

您会发现数据验证的下拉列表类型为 8 ,其名称类似于下拉1
实验将产生工作表中所有形状的类型。