VBA Variable Question

EdE

Board Regular
Joined
Apr 8, 2002
Messages
241
This is a 3 parter. Sorry but I am kind of new to the VBA aspect.
I have a macro that automatically opens 21 files to update. How do I make the filenames, passwords, and tab names variables? I want to put them all at the top of the macro so I only have to change once instead of 21 times. Kind of like making a template.

Thanks!!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

Try something like the following:

Code:
Sub test()
Dim MyArr(1 To 21), x
    
MyArr(1) = "MyBook1"
MyArr(2) = "MyBook2"

' and so on...

For x = 1 To 21
   Workbooks.Open FileName:=MyArr(x)
Next x

End Sub

Note that this may require the full name and path of the workbook.

You can then reference the files by their array index later in the routine.

HTH,
Jay

Edit: Amended the Filename:= reference
This message was edited by Jay Petrulis on 2002-05-01 09:01
 
Upvote 0
Thanks Jay. Is there a way to do the passwords too? Or would it be the same way?
Thanks!
 
Upvote 0
You can do the same

Better yet, make your array multidimensional

Dim MyArray(1 to 21, 1 to 2)

MyArray(1,1) = "MyBook1"
MyArray(1,2) = "MyPassword1"

Then, in the loop Filename:=MyArr(x,1) Password:=MyArray(x,2)

and the like. You may have to tinker a bit, but I believe this is well worth the effort to learn.

Bye,
Jay
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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
Back
Top