我已經繼承了幫助票證來解決Excel宏文檔中的錯誤問題。當運行下面列出的第一個代碼組時,我收到錯誤「運行時錯誤438:對象不支持此屬性或方法」。在進行小的語法更改後,當運行下面列出的第二個代碼組時,會收到錯誤「編譯錯誤:下一個不適用」。我嘗試了一些可能的修復方法,但我無法動搖這些錯誤。任何幫助表示讚賞。Excel宏編碼中的錯誤
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
第二個代碼:在下一個TabNum之前加上'End If' –
爲什麼還要檢查複選框所在的狀態?只需將其設置爲True即可。 – Jeeped
您不應該從Worksheet_Change事件宏中選擇工作表。請參閱[如何避免在Excel VBA宏中使用選擇](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)獲取遠離依賴的方法選擇並激活以實現您的目標。 – Jeeped