我有一个Excel工作表,其中包含14个带复选框的条目。我想将这些复选框的状态读入Visual Basic中的相应复选框。但是,我正在为这种访问而苦苦挣扎。我相信我只能按名称访问Excel工作表中的复选框,因为我不认为他们有像Visual Basic复选框那样的索引,所以我想我必须单独检查所有14个。所以有两个问题:
1)是否我必须按名称引用Excel复选框,因为它们与特定索引无关,正确(这也意味着14条单独的访问行,而不是某种for循环) ?
2)如何在Vsual Basic程序中实际读取复选框的值?我试过这个:
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
oExcel = New Excel.Application 'Create a new instance of Excel
oExcel.Workbooks.Open("C:\QA Controller Test Files\" & SelectController.Text & ".xlsm")
oSheet = oExcel.Worksheets(1)
Dim checked As Boolean = oSheet.Shapes("Verify USB Revision Reporting").ControlFormat.Value
If checked Then TestSelection.CheckedItems(1) = True
我收到一条错误消息,上面写着" Interface' Microsoft.Office.Interop.Excel.Shapes'无法编入索引,因为它没有默认属性。
答案 0 :(得分:0)
您必须遍历工作表上的所有形状并测试每个形状以确保您有一个复选框。这是一个例子(我的工作站上没有Visual Basic,但这应该有效):
Option Explicit
Sub GetCheckBoxes()
Dim oSheet As Worksheet
Dim oShape As Shape
Set oSheet = ActiveSheet
For Each oShape In oSheet.Shapes
If (oShape.Type = msoFormControl) And (oShape.FormControlType = xlCheckBox) Then
If oShape.ControlFormat.Value = 1 Then
Debug.Print "box '" & oShape.Name & "' is checked!"
Else
Debug.Print "box '" & oShape.Name & "' is not checked!"
End If
End If
Next oShape
End Sub