Copy VBA code from one worksheet to another using VBA code

Ok, here's what I want to do: I'm trying to copy all the VBA code from "Sheet2" to the "Sheet 3" panel. I do NOT mean copying a module from one to another, but the code of the excel sheet object.

I already added a link to MS VB for application extensibility 5.3

I’m not sure where to start, but this is where I started, and it will not go anywhere and, probably, it’s not so. Please help - just want to programmatically copy the vba code to another vba sheet.

Dim CodeCopy As VBIDE.CodePane
Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Sheet2").VBE
ActiveWorkbook.VBProject.VBComponenets("Sheet3").CodeModule = CodeCopy
+1
source share
2 answers

CodeModule CodePane, ( ).

Sub test()

Dim CodeCopy As VBIDE.CodeModule
Dim CodePaste As VBIDE.CodeModule
Dim numLines As Integer

Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Sheet2").CodeModule
Set CodePaste = ActiveWorkbook.VBProject.VBComponents("Sheet3").CodeModule

numLines = CodeCopy.CountOfLines
'Use this line to erase all code that might already be in sheet3:
'If CodePaste.CountOfLines > 1 Then CodePaste.DeleteLines 1, CodePaste.CountOfLines

CodePaste.AddFromString CodeCopy.Lines(1, numLines)
End Sub

" MS VB 5.3"

VBA.

Excel 2007+ "" , " " "". , " " VBA .

+10

! , "b" - , .CodeName, NOT .Name

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

:

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

:

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

Source: https://habr.com/ru/post/1660918/


All Articles