How do I declare an ARRAY for filenames?

jumpingbean

New Member
Joined
Dec 12, 2005
Messages
8
Hi...

I have 10 Excel files that I would like to declare in an array. This array will then be used to run in my VBA code. Currently, the code mention each file repetively. What would be the easiest way to declare the files an array?

For example thess are the files.
state1.xls
state2.xls
state3.xls
state4.xls
state5.xls
.....and so on until state10.xls

Thanks a bunch for your help and continue to enjoy life!
jumpingbean
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
jumpingbean

Can we see your code?

You might not actually need an array.
 
Upvote 0
Hi Norie:

This code is intended to VLOOKUP values in Excel file.
Currently, I am referencing each Excel file in the formula.
Isn't there a way I can list all files referenced in my VLOOKUP formula instead of mentioning each one individually.
Suppose I had 50 Excel files to VLOOKUP, replicating each one individually would seem redundant.
Please advise and thanks a bunch.

Sub lookupvalues()

'1) Loop over Cells (x,3)
For x = 2 To 10000

'2) Select active cell
Cells(x, 3).Select
ActiveCell = ""

'3) Check State1.xls
If Cells(x, 3) = "" Then

ActiveCell.FormulaR1C1 = _
"=IF(ISNA(VLOOKUP(RC[-2],[state1.xls]Sheet1!C1,1,FALSE)),"""",VLOOKUP(RC[-2],[state1.xls]Sheet1!C1:C3,3,FALSE))"

End If

'''''''''''

'4) Check State2.xls
If Cells(x, 3) = "" Then

ActiveCell.FormulaR1C1 = _
"=IF(ISNA(VLOOKUP(RC[-2],[state2.xls]Sheet1!C1,1,FALSE)),"""",VLOOKUP(RC[-2],[state2.xls]Sheet1!C1:C3,3,FALSE))"

End If
'''''''''''
'5) Check State3.xls
If Cells(x, 3) = "" Then

ActiveCell.FormulaR1C1 = _
"=IF(ISNA(VLOOKUP(RC[-2],[state3.xls]Sheet1!C1,1,FALSE)),"""",VLOOKUP(RC[-2],[state3.xls]Sheet1!C1:C3,3,FALSE))"

End If
'''''''''''

'6) Check State4.xls
If Cells(x, 3) = "" Then

ActiveCell.FormulaR1C1 = _
"=IF(ISNA(VLOOKUP(RC[-2],[state4.xls]Sheet1!C1,1,FALSE)),"""",VLOOKUP(RC[-2],[state4.xls]Sheet1!C1:C3,3,FALSE))"

End If

Next x
End Sub
 
Upvote 0
Try this.
Code:
Sub lookupvalues()
Dim strLookup As String

    For x = 2 To 10000
    
        For i = 1 To 4
            If Cells(x, 3) = "" Then
                strLookup = "VLOOKUP(RC[-2],[state" & i & ".xls]Sheet1!C1,1,0)"
                Cells(x, 3).FormulaR1C1 = "=IF(ISNA(" & strLookup & "),""""," & strLookup & ")"
            End If
        Next i
    Next x
End Sub
 
Upvote 0
Yeah!!

Thanks Norie for helping me condensed the code from a millions lines to five lines.

stay brillant!
jumpingbean :p
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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