我編寫了一個宏,應該在每個工作表上執行一個加載項。該插件是 Sage 特定的插件。我確定發送密鑰應該與我輸入的任何發送密鑰一起使用?但是,它會在所有工作表上運行我的測試“新增文字”,返回原始活動工作表,然後運行加載項與工作表計數一樣多的次數 - 全部在活動工作表上。我還需要添加一些內容,以便它跳過隱藏的工作表......?請幫忙。
Sub CountSheets()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet 'remember which worksheet is active in the beginning
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Application.SendKeys "%XRV%O", True
Application.Wait (Now + #12:00:01 AM#)
ws.Cells(1, 1) = "Created by Yo Castle 7 "
Application.Wait (Now + #12:00:01 AM#)
Next
starting_ws.Activate 'activate the worksheet that was originally active
End Sub
答案1
若要僅在可見工作表上執行宏,您可以新增IF
就在你開始循環之後。然後這將測試是否Visible
屬性為 true,否則跳至下一個。
所以你的程式碼變成:
Sub CountSheets()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet 'remember which worksheet is active in the beginning
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = true Then
ws.Activate
Application.SendKeys "%XRV%O", True
Application.Wait (Now + #12:00:01 AM#)
ws.Cells(1, 1) = "Created by Yo Castle 7 "
Application.Wait (Now + #12:00:01 AM#)
End If
Next
starting_ws.Activate 'activate the worksheet that was originally active
End Sub