GetOpenFile question

  • Thread starter Thread starter gw.boswell
  • Start date Start date
G

gw.boswell

Can someone explain to me why when the following code executes, the
filename shown in the message box for the first iteration is the last
file name selected in the multiple selection. The second iteration
show the first file name selected and continues on in order until the
next to last file name selected.

Dim FileToOpen As Variant

FileToOpen = Application.GetOpenFilename("TableFiles (*.prn), *.prn", ,
"Name of Files to Use", , True)

counter = 1

Do While counter <= UBound(FileToOpen)

MsgBox FileToOpen(counter)

counter = counter + 1

Loop

End Sub

TIA

Garry
 
I don't think it is anything to do with the selection order, I think it is
to do with the OS, and where it thinks they are. When you see a list of
files, it is always ordered in some way, but that has nothing to do with the
way that it is stored.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
OK, I can accept that. However, I need to address the file names in
order. Any suggestions as to how to do that?
 
You could sort the array

Dim FileToOpen As Variant
Dim counter As Long
FileToOpen = Application.GetOpenFilename("TableFiles (*.prn), *.prn", , _
"Name of Files to Use", , True)

Call ShellSort(FileToOpen)

counter = 1

Do While counter <= UBound(FileToOpen)

MsgBox FileToOpen(counter)

counter = counter + 1

Loop

End Sub

Public Sub ShellSort(ByRef aryToSort)
Dim i As Long, j As Long
Dim iLow As Long, iHigh As Long
Dim tmp As Variant
iLow = LBound(aryToSort)
iHigh = UBound(aryToSort)
j = (iHigh - iLow + 1) \ 2
Do While j > 0
For i = iLow To iHigh - j
If aryToSort(i) > aryToSort(i + j) Then
tmp = aryToSort(i)
aryToSort(i) = aryToSort(i + j)
aryToSort(i + j) = tmp
End If
Next i
For i = iHigh - j To iLow Step -1
If aryToSort(i) > aryToSort(i + j) Then
tmp = aryToSort(i)
aryToSort(i) = aryToSort(i + j)
aryToSort(i + j) = tmp
End If
Next i
j = j \ 2
Loop
End Sub


--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
Back
Top