ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   GetOpenFilename type mismatch (https://www.excelbanter.com/excel-programming/371489-getopenfilename-type-mismatch.html)

XLHead

GetOpenFilename type mismatch
 
Hi,

I am new to this group (and programming).

I have a userform with a listbox that is to be populated with user
selected filenames.

When the user clicks a button on the worksheet I have the the userform
open, the Activate event calls a macro with GetOpenFilenames
(Multiselect = True).

I have a variable called arrFileNames as Variant for the array of file
names.

When I try to add the names to the ListBox I get runtime error 13 -
Type mismatch... sometimes. If the Open dialog box opens and I just
click open without selecting a file it will sometimes put the default
selected file name in the ListBox.


Here is the code that is called from the UserForm_Activate() event:

Private Sub cmdGet_Click()
' Get list of files to import and put in list box.

Dim i As Integer
Dim arrFileNames As Variant

arrFileNames = Application.GetOpenFilename(, 1, _
"Select One Or More Files To Open", , True)

If TypeName(arrFileNames) = "Boolean" Then Exit Sub 'cancel
pressed

For i = LBound(arrFileNames) To UBound(arrFileNames)
lbxFiles.AddItem funcRemovePath(CStr(arrFileNames(i)))
lbxFiles.List(lbxFiles.ListCount - 1, 1) = CStr(arrFileNames(i))
Next i

End Sub


I am getting the error at the LBound(arrFileNames) line.

Am I doing something wrong or is this just some strange bug? It's
driving me nuts!


Jon Peltier

GetOpenFilename type mismatch
 
I've never encountered this that I can recall, and I use GetOpenFileName a
lot. If you only return one file, it should be an array with one element,
but I've never explicitly tried hitting Enter without selecting a file
first. You could check TypeName(arrFileNames) for Variant() or just
IsArray(arrFileNames) before you loop through the array.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"XLHead" wrote in message
ups.com...
Hi,

I am new to this group (and programming).

I have a userform with a listbox that is to be populated with user
selected filenames.

When the user clicks a button on the worksheet I have the the userform
open, the Activate event calls a macro with GetOpenFilenames
(Multiselect = True).

I have a variable called arrFileNames as Variant for the array of file
names.

When I try to add the names to the ListBox I get runtime error 13 -
Type mismatch... sometimes. If the Open dialog box opens and I just
click open without selecting a file it will sometimes put the default
selected file name in the ListBox.


Here is the code that is called from the UserForm_Activate() event:

Private Sub cmdGet_Click()
' Get list of files to import and put in list box.

Dim i As Integer
Dim arrFileNames As Variant

arrFileNames = Application.GetOpenFilename(, 1, _
"Select One Or More Files To Open", , True)

If TypeName(arrFileNames) = "Boolean" Then Exit Sub 'cancel
pressed

For i = LBound(arrFileNames) To UBound(arrFileNames)
lbxFiles.AddItem funcRemovePath(CStr(arrFileNames(i)))
lbxFiles.List(lbxFiles.ListCount - 1, 1) = CStr(arrFileNames(i))
Next i

End Sub


I am getting the error at the LBound(arrFileNames) line.

Am I doing something wrong or is this just some strange bug? It's
driving me nuts!





All times are GMT +1. The time now is 03:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com