1

i have got this code:

Sub openMyStatsData()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename '(FileFilter:="Excel Files,*.xlsx*;*.xlsm*")

If fileName <> False Then
    Workbooks.Open (myFileName)
End If

End Sub

the dialog window opens and it let me browse and choose the file i want but when i click on the Open button nothing else happens. i need to be able to browse for the file and open it.

I am new to VBA in case you were wondering. thanks!

1
  • 3
    State 'Option Explicit' at the top of your module (before the rest of the code) and see if you can work it out from there...
    – Dave
    Commented Oct 18, 2016 at 12:21

2 Answers 2

4

Change your code to this one:

Sub openMyStatsData()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename '(FileFilter:="Excel Files,*.xlsx*;*.xlsm*")

If myFileName <> False Then
    Workbooks.Open (myFileName)
End If

End Sub

And make sure that you always have Option Explicit on top, to avoid errors like this one.

2

You'd called the variable "myFileName" but then in the If statement you wrote "fileName". You have to change it to "myFileName".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.