我继承了一个帮助票据来解决Excel宏文档中的错误问题。当运行下面列出的第一个代码组时,我收到错误“运行时错误438:对象不支持此属性或方法”。在进行小的语法更改后,当运行下面列出的第二个代码组时,我收到错误“编译错误:下一个没有For”。我尝试了一些可能的修复,但我无法摆脱这些错误。任何帮助表示赞赏。
Private Sub Worksheet_Change(ByVal target As Range)
Dim TabNum As Long
' Check all automation boxes on all of the tabs when Master is selected
If Range("Master") = "Master" Then
For TabNum = 7 To 23 Step 1 'thisworkbook.Sheets.Count is the number of tabs in the open master SIG
ThisWorkbook.Worksheets(TabNum).Select
If ThisWorkbook.Worksheets(TabNum).CheckBox1.Value = False Then ThisWorkbook.Worksheets(TabNum).CheckBox1.Value = True
Next TabNum
End If
' move back to the formula notes worksheet
ThisWorkbook.Worksheets(27).Select
End Sub
Private Sub Worksheet_Change(ByVal target As Range)
Dim TabNum As Long
' Check all automation boxes on all of the tabs when Master is selected
If Range("Master") = "Master" Then
For TabNum = 7 To 23 Step 1 'thisworkbook.Sheets.Count is the number of tabs in the open master SIG
ThisWorkbook.Worksheets(TabNum).Select
If ThisWorkbook.Worksheets(TabNum).CheckBox1.Value = False Then
ThisWorkbook.Worksheets(TabNum).CheckBox1.Value = True
Next TabNum
End If
' move back to the formula notes worksheet
ThisWorkbook.Worksheets(27).Select
End Sub
答案 0 :(得分:1)
从您的简短说明中,您似乎想循环浏览队列中的工作表(从第7位到第23位,包括第7位),并将 Checkbox1 设置为 True 每个工作表。您希望此触发'...当选择“主”(而不是已更改)时,Worksheet_SelectionChange事件宏比Worksheet_Change更合适。
Private Sub Worksheet_SelectionChange(ByVal target As Range)
Dim tabNum As Long
' Check all automation boxes on all of the tabs when Master is selected
If LCase(target(1).Value2) = "master" Then
For TabNum = 7 To 23 Step 1 'thisworkbook.Sheets.Count is the number of tabs in the open master SIG
ThisWorkbook.Worksheets(tabNum).CheckBox1.Value = True
Next tabNum
End If
End Sub
请注意,删除的代码比添加或编辑的代码要多得多。
¹有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros。