将VBA代码从一个工作表复制到工作簿中的所有其他工作表

时间:2018-03-23 19:02:20

标签: vba copy

我的工作簿中有代码需要复制到工作簿中的所有工作表,所以当我在一张工作表上更新代码时,我不需要手动复制/粘贴到所有其他工作表

代码位于名为“内容”的主表上

或者,如果我可以获得帮助,将代码应用于“ThisWorkbook”中的所有工作表(我确实尝试过这个选项,但是我无法让它工作)

这是我需要应用于所有工作表的代码:

Private Sub CommandButton1_Click()
If ComboBox3.Value <> "" Then
    Worksheets(ComboBox3.Value).Activate
ElseIf ComboBox3.Value = "" And ComboBox2.Value <> "" Then
    Worksheets(ComboBox2.Value).Activate
Else:
    Worksheets(ComboBox1.Value).Activate
End If
End Sub

Private Sub ComboBox2_Change()
Dim rngMenu2 As Range
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long

    ' check that a Menu1 has been selected
  If ComboBox2.ListIndex <> -1 Then
   ComboBox3.Clear
       strSelected = ComboBox2.Value

       LastRow = Worksheets("Contents").Range("F" & Rows.Count).End(xlUp).Row

       Set rngList = Worksheets("Contents").Range("F1:F" & LastRow)


       For Each rngMenu2 In rngList

             If rngMenu2.Value = strSelected Then

                  ComboBox3.AddItem rngMenu2.Offset(, 1)

             End If

       Next rngMenu2

  End If
End Sub



Private Sub ComboBox1_Change()
Dim rngMenu1 As Range
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long

    ' check that a Menu1 has been selected
  If ComboBox1.ListIndex <> -1 Then
   ComboBox2.Clear
   ComboBox3.Clear
       strSelected = ComboBox1.Value

       LastRow = Worksheets("Contents").Range("D" & Rows.Count).End(xlUp).Row

       Set rngList = Worksheets("Contents").Range("D1:D" & LastRow)


       For Each rngMenu1 In rngList

             If rngMenu1.Value = strSelected Then

                  ComboBox2.AddItem rngMenu1.Offset(, 1)

             End If

       Next rngMenu1

  End If

End Sub

这是我必须将代码复制到另一张纸的代码,但它只复制到1张,我该如何将其更改为所有纸张?在将新代码复制到工作表之前,我还需要删除任何现有代码... 我有一个范围

的工作表名称列表
Private Sub CommandButton2_Click()
Dim CodeCopy As VBIDE.CodeModule
Dim CodePaste As VBIDE.CodeModule
Dim numLines As Integer
Dim rngList As Range
Dim LastRow As Long


       LastRow = Worksheets("Contents").Range("H" & Rows.Count).End(xlUp).Row

       Set rngList = Worksheets("Contents").Range("H1:H" & LastRow)

Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
Set CodePaste = ActiveWorkbook.VBProject.VBComponents("Sheet40").CodeModule

End Sub

对此的任何帮助都很棒(注意:我是一个有编码的新手!:)

提前致谢!

0 个答案:

没有答案