PDA

View Full Version : Copy VBA code



mdmackillop
01-13-2006, 12:06 PM
Hi all,
I've 6 users each using a copy of the same workbook. Although most of the code is contained in a shared XLA file, it's a pain modifying the code in each of the books in turn. I'm looking to automate this by copying code from a source workbook (WBS) to each of the destination workbooks in turn (WBD). I'm collecting the corresponding module codenames in the array.
My problem lies in writing to the destination workbook. It crashes Excel. Any suggestions?
Trust Access to VB Project is enabled.
Regards
MD
For i = 0 To 5
With WBS.VBProject.VBComponents(arrMod(i, 0)).CodeModule
NewCode = .Lines(1, .countoflines)
End With
With WBD.VBProject.VBComponents(arrMod(i, 1)).CodeModule
'tried
.AddFromString NewCode
'and
.InsertLines 1, NewCode
End With
NewCode = ""
Next

Bob Phillips
01-13-2006, 12:33 PM
Martin,

Both methods work fine for me. Admittedly, I removed the loop and the array, and hard-coded the module names, but that aside ...

mdmackillop
01-13-2006, 01:07 PM
Hi Bob,
I made up a sub routine as a string and the code ran fine. The problem seems to be writing the code that has been saved in the NewCode variable.
I've zipped up all my files. They need to be saved into C:\QS\Timesheets\
Edit:
Open and click the big button on TimeSheetCode. It should copy its code into the two folder files. The other copy is a "spare" to replace tested files.