简体   繁体   English

一次运行多个宏(不同的工作表)

[英]Running multiple macros at once (different sheets)

I am running multiple macros using VB for two different sheets in Excel. 我正在VB中为Excel中的两个不同工作表运行多个宏。

Sheet1: I can run multiple macro's together: Sheet1:我可以一起运行多个宏:

Sub option2()
    CreateTable2
    HeaderChange1
    HeaderChange2
    HeaderChange3
    HeaderChange4
End Sub

Sheet 2: I can run multiple macros together (this presents the new data that the macro from sheet 1 has created): 工作表2:我可以一起运行多个宏(这显示工作表1中的宏创建的新数据):

Sub option1()
    ClearCols
    CreateTable
    MoveColumns
End Sub

Is there any way I can run one macro to complete all the functions at once - instead of running the two above separately? 有什么方法可以运行一个宏一次完成所有功能-而不是分别运行上面的两个?

First, you should replace all your activesheet references to a specific worksheet, add a worksheet info to all the ranges you have without a worksheet info, eg 首先,您应该将所有活动工作表引用替换为一个特定的工作表,将工作表信息添加到您没有工作表信息的所有范围内,例如

activesheet.range("A1").copy becomes Sheets("Sheet1").range("A1").copy activesheet.range("A1").copy变为Sheets("Sheet1").range("A1").copy

range("A1").copy becomes Sheets("Sheet1").range("A1").copy range("A1").copy变为Sheets("Sheet1").range("A1").copy

Then you can either combine all your macros or run them in one combined macro: 然后,您可以合并所有宏或在一个合并的宏中运行它们:

Sub RunAllMacros
CreateTable2
HeaderChange1
HeaderChange2
HeaderChange3
HeaderChange4
ClearCols
CreateTable
MoveColumns
End Sub

If you still run off the active sheet, you can change the selection in the middle (though selecting in VBA should be avoided unless you want the user to see a selection change) 如果仍然用完活动工作表,则可以在中间更改选择(尽管应避免在VBA中进行选择,除非您希望用户看到选择更改)

Sub RunAllMacros

CreateTable2
HeaderChange1
HeaderChange2
HeaderChange3
HeaderChange4

Sheets("Sheet2").select

ClearCols
CreateTable
MoveColumns
End Sub

It seems to me that you can create a new macro to run both options... 在我看来,您可以创建一个新的宏来运行这两个选项...

Sub bothOptions()
    Call Option1
    Call Option2
End Sub

Is this what you needed? 这是您所需要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM