![Excel 宏没有按照应有的顺序运行](https://linux22.com/image/1615606/Excel%20%E5%AE%8F%E6%B2%A1%E6%9C%89%E6%8C%89%E7%85%A7%E5%BA%94%E6%9C%89%E7%9A%84%E9%A1%BA%E5%BA%8F%E8%BF%90%E8%A1%8C.png)
我编写了一个宏,它应该在每个工作表上运行一个插件。该插件是 Sage 专用插件。我确定 sendkeys 应该可以与我输入的任何 sendkeys 配合使用?但是,它会在所有工作表上运行我的测试“添加文本”,返回到原始活动工作表,然后根据工作表数量多次运行插件 - 全部在活动工作表上。我还需要添加一些内容以便它跳过隐藏的工作表......?请帮忙。
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
属性为真,否则跳至下一个。
所以你的代码变成:
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