using vba to copy array constants names and definitions from one workbook to another

Joined
Jan 12, 2013
Messages
1
I have two workbooks, one for last year's dataset and another for this years. I use a lot of array constants. I would like to copy the array constant names and definitions from one workbook to the other. How can I do this in VBA?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

tlowry

Well-known Member
Joined
Nov 3, 2011
Messages
1,367
Welcome to MrExcel,

My approach to this would be to create a Module that has the information you wish to pass from one workbook to another. Export the module (after any changes that are needed). For this demo, I named the Module “ArraysToSave” and exported it to a file: "c:\temp\arr.bas". You could do this manually, but where’s the fun in that?


To export with VBA:

Code:
ActiveWorkbook.VBProject.VBComponents("ArraysToSave").export "c:\temp\arr.bas"

To import that file to another workbook,:

Code:
ActiveWorkbook.VBProject.VBComponents.Import "c:\temp\arr.bas"
Application.Run "SetupArr"

Where SetupArr is a sub to do any legwork to get the arrays stuffed with constants and is in the file "c:\temp\arr.bas"

My arr.bas is:

Code:
Public arr
Sub SetupArr()
    arr = Array("Dog", "Horse", "Rain")
End Sub
Sub exportArr()
    ActiveWorkbook.VBProject.VBComponents("ArraysToSave").export "c:\temp\arr.bas"
End Sub

So, after the import and running SetupArr, the array arr is stuffed with "Dog", "Horse", "Rain" and may be used in the “normal” array statements.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,193
Messages
5,768,772
Members
425,492
Latest member
blueexcel123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top