GetOpenFilename - a strange stuff

grautu

New Member
Joined
Sep 26, 2004
Messages
38
Hi!
My thread is asking not for a valid code but for any hint/comment for properly understanding the GetOpenFilename method in VBA. The following code does work very well but please notice its second line (Dim MY_FILE_NAME ...) is actually commented out (it will be clear later on why).

Code:
Sub Test_GetOpenFilename()
'   Dim MY_FILE_NAME as ???
    MY_FILE_NAME = Application.GetOpenFilename(FileFilter:="Excel (*.xls), *.xls", Title:="Select File")
    If MY_FILE_NAME <> False Then
                ' Here is a completely mind bogging issue: if choosing "=True" instead of "<>False",
                ' then the code will fail to work as expected!!!
       Workbooks.Open Filename:=MY_FILE_NAME
       ThisWorkbook.Sheets(1).Cells(1, 1).Value = MY_FILE_NAME  'Just for seeing
                                                                'how the concrete 
                                                                'value of MY_FILE_NAME
                                                                'looks like
    Else
       MsgBox "No file selected"
    End If
End Sub

REMARKS
-------
(1) The above macro does work and its output (at cells (1,1)) is the full path to the selected filename if the case. See the command ThisWorkbook.Sheets(1).Cells(1, 1).Value = MY_FILE_NAME
(2) If questioning above
Code:
If MY_FILE_NAME = True
instead of ... <> False, then the code wil fail to work properly in that only the Else branch will allways be chosen. Why? I have no idea.
(3) Furthermore, whenever trying to activate the above command
Code:
Dim MY_FILE_NAME as ???
with various ??? instances, then my code was always failing to work: that ??? may not be Boolean or String etc etc. Most likely it must be an Object but I was not able to learn which one, concretely.
==================

QUESTIONS
---------
(1) Is there any valid choice for ??? at Dim MY_FILE_NAME as ??? above?
(2) How to explain the fact that the intuitively correct variant MY_FILE_NAME = True fails to work as expected?
==================

Any comment is kindly appreciated, grateful thanks in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
1 Try Variant, or just omit the data type and it'll default to that.

2 Not quite sure what you mean. If you select files then a string is returned not a boolean value.
 

Forum statistics

Threads
1,141,144
Messages
5,704,541
Members
421,353
Latest member
jekoxien15

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
Top