Copy VBA code from one Worksheet to another using VBA code

前端 未结 2 671
独厮守ぢ
独厮守ぢ 2020-12-07 05:12

Ok here is what I want to accomplish: I am trying to copy all the VBA code from \"Sheet2\" to \"Sheet 3\" code pane. I\'m NOT referring to copying a Module from one to anoth

2条回答
  •  眼角桃花
    2020-12-07 05:39

    Thank you all! After testing multiple suggestions above, where "b" is the Worksheet name, you must use .CodeName, NOT .Name

    Set CodePaste = ActiveWorkbook.VBProject.VBComponents(WorkShe‌ets(b).CodeName).Cod‌​eModule
    

    If you have set your target worksheet as an object:

    Dim T As Worksheet  
    Set T = Worksheets("Test")  
    

    Then you simply need:

    Set CodePaste = ActiveWorkbook.VBProject.VBComponents(Worksheets(T.Name).CodeName).Cod‌​eModule
    

提交回复
热议问题