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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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