I'm trying to set a workbook variable from a file name, but getting run time error 9.
I'm trying to understand VBA a bit better, so even if I have a bit of code that works, it feels like it's a hack, and I'd rather know the proper way of coding it - like why I can't get a workbook object returned from the workbooks collection. It's likely painfully obvious what's wrong with the code, and I'll be laughed at, but wiser
I'm trying to understand VBA a bit better, so even if I have a bit of code that works, it feels like it's a hack, and I'd rather know the proper way of coding it - like why I can't get a workbook object returned from the workbooks collection. It's likely painfully obvious what's wrong with the code, and I'll be laughed at, but wiser
Code:
Sub OpenFile()
Dim varFilename As String
Dim inputWorkbook As Workbook
varFilename = Application.GetOpenFilename("Excel Files, *.xl*") 'Get open file dialog, filter on files with *.xl*
If varFilename = "False" Then End 'If no file is selected, end the macro altogether
Workbooks.Open Filename:=varFilename 'Open file
Set inputWorkbook = ActiveWorkbook '<-- Works
Set inputWorkbook = Workbooks(varFilename) '<-- Doesn't work
Set inputWorkbook = Workbooks.Item(varFilename) '<-- Doesn't work either
End Sub