简体   繁体   English

在相同的不同工作表上复制 VBA 代码

[英]Duplicating a VBA code on different Sheets in same

I am trying to run the same code throughout all the sheets on the given workbook.我正在尝试在给定工作簿上的所有工作表中运行相同的代码。 I have first Worksheet in workbook assigned to a VBA Code and formatted with shapes/buttons and have made two identical copies of that sheet.我将工作簿中的第一个工作表分配给 VBA 代码并使用形状/按钮进行格式化,并制作了该工作表的两个相同副本。 However, when the copy was made my VBA code will not work on the copies it only works on the first and original sheet.但是,当制作副本时,我的 VBA 代码不适用于副本,它仅适用于第一张和原始表。 Is there any way to loop the code to run on the buttons/shapes.有什么方法可以循环代码以在按钮/形状上运行。 I don't need the table data transferred it should stay within each sheet.我不需要传输的表格数据,它应该保留在每个工作表中。 The VBA code is only to the table headers and subtabs. VBA 代码仅适用于表标题和子选项卡。 Below is the code I have.下面是我的代码。

    Sub TabCase()
    With Sheet1
    Application.ScreenUpdating = False
    .Shapes("CaseOn").Visible = msoCTrue
    .Shapes("CaseOff").Visible = msoFalse

    .Shapes("DemOn").Visible = msoFalse
    .Shapes("DemOff").Visible = msoCTrue

    .Shapes("RefOn").Visible = msoFalse
    .Shapes("RefOff").Visible = msoCTrue

    .Shapes("SDOHOn").Visible = msoFalse
    .Shapes("SDOHOff").Visible = msoCTrue

    .Range("B:K").EntireColumn.Hidden = False
    .Range("M:AO").EntireColumn.Hidden = True
     Application.ScreenUpdating = True
     End With
     End Sub
     Sub TabDem()
     With Sheet1
     Application.ScreenUpdating = False
    .Shapes("CaseOn").Visible = msoFalse
    .Shapes("CaseOff").Visible = msoCTrue

    .Shapes("DemOn").Visible = msoCTrue
    .Shapes("DemOff").Visible = msoFalse

    .Shapes("RefOn").Visible = msoFalse
    .Shapes("RefOff").Visible = msoCTrue

    .Shapes("SDOHOn").Visible = msoFalse
    .Shapes("SDOHOff").Visible = msoCTrue

    .Range("M:Y").EntireColumn.Hidden = False
    .Range("B:K,AA:AO").EntireColumn.Hidden = True
     Application.ScreenUpdating = True

    End With
    End Sub
    Sub TabRef()
    With Sheet1
    Application.ScreenUpdating = False
    .Shapes("CaseOn").Visible = msoFalse
    .Shapes("CaseOff").Visible = msoCTrue

    .Shapes("DemOn").Visible = msoFalse
    .Shapes("DemOff").Visible = msoCTrue

    .Shapes("RefOn").Visible = msoCTrue
    .Shapes("RefOff").Visible = msoFalse

    .Shapes("SDOHOn").Visible = msoFalse
    .Shapes("SDOHOff").Visible = msoCTrue

    .Range("AA:AE").EntireColumn.Hidden = False
    .Range("B:Z,AF:AO").EntireColumn.Hidden = True
     Application.ScreenUpdating = True

    End With
    End Sub
    Sub TabSDOH()
    With Sheet1
    Application.ScreenUpdating = False
    .Shapes("CaseOn").Visible = msoFalse
    .Shapes("CaseOff").Visible = msoCTrue

   .Shapes("DemOn").Visible = msoFalse
   .Shapes("DemOff").Visible = msoCTrue

  .Shapes("RefOn").Visible = msoFalse
  .Shapes("RefOff").Visible = msoCTrue

  .Shapes("SDOHOn").Visible = msoCTrue
  .Shapes("SDOHOff").Visible = msoFalse

  .Range("AG:AO").EntireColumn.Hidden = False
  .Range("B:AF").EntireColumn.Hidden = True
   Application.ScreenUpdating = True
   End With
   End Sub

   Sub loopAcrossSheets()
   temp = Array("County", "City", "CSV")
    For Each SheetName In temp
   
    Next
    End Sub 

Rework each method to take a parameter of type Worksheet - eg:重新设计每个方法以获取 Worksheet 类型的参数 - 例如:

Sub TabCase(ws As Worksheet)
    Application.ScreenUpdating = False
    With ws
        .Shapes("CaseOn").Visible = msoCTrue
        .Shapes("CaseOff").Visible = msoFalse
        .Shapes("DemOn").Visible = msoFalse
        .Shapes("DemOff").Visible = msoCTrue
        .Shapes("RefOn").Visible = msoFalse
        .Shapes("RefOff").Visible = msoCTrue
        .Shapes("SDOHOn").Visible = msoFalse
        .Shapes("SDOHOff").Visible = msoCTrue
        .Range("B:K").EntireColumn.Hidden = False
        .Range("M:AO").EntireColumn.Hidden = True
     End With
     Application.ScreenUpdating = True
End Sub

Then instead of calling it like:然后而不是像这样称呼它:

TabCase

you'd call it and pass in a worksheet as an argument:您可以调用它并将工作表作为参数传递:

TabCase Sheet1 'or Sheet2, or ThisWorkbook.Sheets("NewSheet") etc

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

相关问题 在工作簿中的所有工作表上应用相同的 VBA 代码 - Applying same VBA code on all sheets in a workbook 如何为不同的列/工作表循环 VBA 代码 - How to loop VBA code for different columns/sheets VBA代码用于在同一Excel工作簿中使用来自两个不同工作表的更新来填充MI电子表格 - VBA Code to populate an MI spreadsheet with updates from two different sheets in the same Excel workbook VBA-Excel从不同位置的不同工作表获取相同的数据 - VBA - Excel Getting same data from different sheets at different positions VBA-从不同的工作表获取具有相同标题的数据 - VBA - getting data that has same header from different sheets 动态筛选器(不同的字符串)用于多个工作表(同一工作簿中的不同工作表)VBA - Dynamic filters (different strings) for multiple sheets (different sheets in the same workbook) VBA Vba代码在同一工作簿的单独工作表中打开多个文件 - Vba code to open multiple files in separate sheets of the same workbook 在所有活动工作表中应用相同的Excel-VBA代码 - Applying Same Excel-VBA Code in All Active Sheets 在具有不同行数的所有图纸上运行VBA格式代码 - Run VBA format code on all sheets with different number of rows VBA 代码循环遍历不同的工作表并获取特定的列 - VBA code to loop through different sheets and take specific columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM