Store file names in global array variable

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Store file names in global array variable

Post by Rudi »

Hi,

I would like to run a prompt to collect multiple files, and store it in a variable.
Then I have several other macros that must cycle through this file list and run their actions to process the files.
The person will want to run these several macros in different combinations, so when they run one it will run looping through the files list. Later, run a different macro and it will loop through the same files.
Ideally the files list will only be reset when they close the WB that contains the macros, or if they run the below macro again, to choose a different set of files.

So in essence, the below macro must store the files selected and other macros will refer to the variable and loop through the file list processing things in these files.
Will the macro below create the file list variable, and how do I store it "permanently" until the WB closes or it reruns?
TX

Code: Select all

Option Explicit
Public varResult As Variant, i As Integer

Sub OpenWBs()
   varResult = Application.GetOpenFilename _
               (FileFilter:="Excel Files, *.xls*", MultiSelect:=True)
   If Not IsArray(varResult) Then
      MsgBox "No file was selected.", vbExclamation
      Exit Sub
   End If
   For i = LBound(varResult) To UBound(varResult)
   Next i
End Sub

Sub Test()
   For i = LBound(varResult) To UBound(varResult)
      Application.StatusBar = "Processing file: " & i & " of " & UBound(varResult)
      Workbooks.Open Filename:=varResult(i), UpdateLinks:=False
      sActWB = ActiveWorkbook.Name
      'Do stuff in WB
      Workbooks(sActWB).Close True
   Next i
   Application.StatusBar = False
End Sub

Sub Test2()
   Workbooks.Open Filename:=varResult(3), UpdateLinks:=False
   sActWB = ActiveWorkbook.Name
   'Do stuff in WB(3)
   Workbooks(sActWB).Close True
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Store file names in global array variable

Post by HansV »

The code that you posted will fill the array variable varResult, and this will remain available until either the workbook is closed, or an unhandled error occurs in some VBA code (that will reset all variables).

If you want to store the list of files more permanently, you can copy it into a range of cells, for example in a hidden worksheet. Sample code:

Code: Select all

   Worksheets("HiddenSheet").Range("A:A").ClearContents
   Worksheets("HiddenSheet").Range("A1").Resize(RowSize:=UBound(varResult)).Value = _
      Application.Transpose(varResult)
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Store file names in global array variable

Post by Rudi »

How about that...excellent!
TX for confirming.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Store file names in global array variable

Post by Rudi »

OK, I have a subscript out of range?
Seems the file names are not passing through?
Also, I selected 3 files, but the value i is equal to 4?
SP2-Sat,12-10.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Store file names in global array variable

Post by Rudi »

Ah....
I used the Locals Window which cleared up the variable issue...

Code: Select all

Option Explicit
Public varResult As Variant, i As Integer, sActWB As String

Sub OpenWBs()
   varResult = Application.GetOpenFilename _
               (FileFilter:="Excel Files, *.xls*", MultiSelect:=True)
   If Not IsArray(varResult) Then
      MsgBox "No file was selected.", vbExclamation
      Exit Sub
   End If
End Sub

Sub Test()
   For i = LBound(varResult) To UBound(varResult)
   Application.StatusBar = "Processing file: " & i & " of " & UBound(varResult)
   Workbooks.Open Filename:=varResult(i), UpdateLinks:=False
   sActWB = ActiveWorkbook.Name
   'Do stuff in WB
   Workbooks(sActWB).Close True
   Next i
End Sub

Sub Test2()
   Workbooks.Open Filename:=varResult(3), UpdateLinks:=False
   sActWB = ActiveWorkbook.Name
   'Do stuff in WB(3)
   Workbooks(sActWB).Close False
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Store file names in global array variable

Post by HansV »

After the For i ... Next i loop, i will be one more than the highest loop index, so it makes no sense to use it "as is" - it doesn't correspond to an array element any more.
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Store file names in global array variable

Post by Jan Karel Pieterse »

If you are planning to use i as a loop counter in different routines, best NOT to declare it in the dclaration section of the module, but rather in each sub in turn. That keeps them separate.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Store file names in global array variable

Post by Rudi »

Jan Karel Pieterse wrote:If you are planning to use i as a loop counter in different routines, best NOT to declare it in the dclaration section of the module, but rather in each sub in turn. That keeps them separate.
Noted.
TX for the advice.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.