Excelmacros编码错误

我已经inheritance了一个帮助票证来解决Excelmacros文档中的错误问题。 当运行下面列出的第一个代码组时,收到错误“运行时错误438:对象不支持此属性或方法”。 在进行小的语法更改后,运行下面列出的第二个代码组时,将收到错误“编译错误:下一个不适用”。 我已经尝试了一些可能的修复,但我不能动摇这些错误。 任何帮助表示赞赏。

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 

错误438

 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 

错误下一个没有

从您的简要说明中,您似乎要循环查看队列中的工作表(从位置7到位置23),并在每个工作表上将Checkbox1设置为True 。 你想要这个触发的'…当主被选中' (没有改变 ),所以一个Worksheet_SelectionChange事件macros比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 

请注意,删除的代码数量比添加或编辑的要多得多。


¹ 请参阅如何避免使用在Excel VBAmacros中select更多的方法来摆脱依靠select和激活来实现您的目标。