How to create variable length array to store strings?

Huby00

New Member
Joined
Nov 14, 2011
Messages
36
Hello, I have one question regarding variable arrays to store string.
I'm trying to have the user enter the length of an array, and store a string (file name) into the array for use later. However, I get an error stating "constant expression required". Tried two different ways to set the variable length but no dice.

Code:
[B][I][U]First try[/U][/I][/B]
[B][I][/I][/B] 
    Dim strFile As String
    Dim lNum As Integer
    
    lNum = Application.InputBox _
             (Prompt:="How many files would you like to compare?", _
                    Title:="Hub's Trace", Type:=1)
        
    For i = 0 To lNum - 1
    strFile = Application.GetOpenFilename
    myFiles(i) = strFile     <-------- [COLOR=#ff0000]error: sub or function not defined [/COLOR]
[COLOR=#ff0000] [/COLOR]
    Next i

Code:
[B][I][U]second try[/U][/I][/B]
    Dim strFile As String
    Dim lNum As Integer
    
    lNum = Application.InputBox _
             (Prompt:="How many files would you like to compare?", _
                    Title:="Hub's Trace", Type:=1)

    Dim myFiles(1 To lNum) As Variant<------ [COLOR=red]error occurs here [/COLOR]

    For i = 1 To lNum Step 1
    myFiles(i) = i + 1
    Next i
    
    Cells(2, 3) = apllication.CountA(myFiles) 'to see how many values are stored: confirming code
    
    Range("A1") = myFiles  'to list values stored in the array


any help would be very appreciated.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Thanks for the reply.

ReDim myfiles(1 To lNum) works! i get an error message later though. But i had a question between

ReDim myfiles(1 To lNum) and
ReDim Preserve myfiles(1 To lNum)

The second line of code gives me an error saying type mismatch. I was just wondering why it does that.

THe error i have is when i try to return a value to verify the code is doing what it is supposed to do:


ReDim myfiles(1 To lNum)

For i = 1 To lNum Step 1
myfiles(i) = i + 1
Next i

Cells(4, 6) = apllication.CountA(myfiles) <=-=== returns error stating object is required
 
Upvote 0
That works to check that the variable array is of the correct length, thank you. last part i need help on, i get an error below:

lNum = Application.InputBox _
(Prompt:="How many files would you like to compare?", _
Title:="Hub's Trace", Type:=1)

ReDim myfiles(1 To lNum)

For i = 0 To lNum - 1
strFile = Application.GetOpenFilename
myfiles(i) = strFile <==== subscript out of range
Next i

THank you sir.
 
Upvote 0
As yuou have defined strfile as 1 to n then it needs to be

Code:
For i = 1 To lNum
strFile = Application.GetOpenFilename
myfiles(i) = strFile
Next i
 
Upvote 0
Ah.. i was just in the middle of editing my post... thank you VoG. You are "Very originally good"...
 
Upvote 0
Out of curiosity, why does Range(Cells(1, 1), Cells(UBound(myfiles), 1)) = myfiles copy the same result for each cell? I changed this line of code to
Code:
   For i = 1 To lNum
    Cells(i, 1) = myfiles(i)
    Next i

but would like to know how to output the entire array without a for loop for future reference. Code is below.


For i = 1 To lNum
strFile = Application.GetOpenFilename

For j = i To 1 Step -1
While strFile = myfiles(j)
MsgBox ("You all ready entered this file. Try again.")
strFile = Application.GetOpenFilename
Wend
Next j

myfiles(i) = strFile
Next i

Range(Cells(1, 1), Cells(UBound(myfiles), 1)) = myfiles

End Sub

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,555
Members
449,170
Latest member
Gkiller

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