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

标签 vba excel

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

 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单元格属性以查看它是否与包含“真”的行匹配。
这可能吗?我到目前为止:
 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

最佳答案

你走在正确的轨道上。首先找到包含“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

关于vba - 如何确定包含各种形状的工作表上的形状类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091963/

相关文章:

Excel VBA : How to clear contents for specified cells when another cell contains specific text or string

vba - Excel 宏,在运行时插入国际有效的公式

c# - 在 GridView 中使用自动生成的列提取列数和标题文本

vba - 循环为一行中的每个新数据创建一个新工作表 - MS Excel

vba - 在电子表格的左上角显示选择

vba - 如何在 ms access vba 中运行追加查询作为事务的一部分

Excel VBA刷新以只读方式打开的文档

excel - 为什么 Left 函数返回运行时错误 '424' ?

excel - 我怎样才能让它从第41行开始呢?

vba - 添加图像作为评论 VBA