Excel 宏没有按照应有的顺序运行

Excel 宏没有按照应有的顺序运行

我编写了一个宏,它应该在每个工作表上运行一个插件。该插件是 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

相关内容