[英]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.