VBA: Using a variable as a Workbook name

dha17

Board Regular
Joined
May 17, 2004
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hi,

I currently use a variable to open a filename (which works fine):

Dim FileName As Variant
FileName = Application.GetOpenFilename()
Workbooks.Open filename:=FileName

I would like to use that same variable as the filename of a workbook, but it does not work:

Workbooks(FileName).Worksheets("Sheet1").Range("A:T").ClearContents

Any ideas of how to use this variable as the filename?

Denny
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Dim FilePath As String, FileName As String

FilePath = Application.GetOpenFilename()
FileName = Mid(FilePath, InStrRev(FilePath, "\") + 1)
Workbooks.Open FileName:=FilePath
Workbooks(FileName).Worksheets("Sheet1").Range("A1") = "hello"


Note you still need some error checking to ensure the person has actually selected a file and not simply closed the GetOpenFilename() dialog. Replace the "hello" bit with your clear contents code.

Regards,

Alex
 
Upvote 0
Code:
FilePath = Application.GetOpenFilename()
If FilePath <> False Then
    'Is Acceptable
Else
    'Is NOT Acceptable - pressed Cancel
End If
 
Upvote 0
Hi Juan, :)

Can you give an example? I've been using that for some months mow without consequence. (If I need to switch, I'd like to do it sooner rather than later!)

Juan Pablo González said:
I prefer to use

If TypeName(FilePath) = "Boolean" Then
'Canceled
Else

End If

to avoid mismatch errors.
 
Upvote 0
I tried both methods and was unsuccessful. I could also use an example for Juan's method.

However, I was able to make it work by placing quotations around the word False:

If FilePath = "False" Then
MsgBox "Cancelled"
Exit Sub
End If
 
Upvote 0
Zack,

Try these 3 examples. The most "notorious" one is Test3.

Code:
Sub Test1()
   Dim Ans As String
   
   Ans = Application.GetOpenFilename
   If Ans = False Then
      MsgBox "Cancelled"
   End If
End Sub

Sub Test2()
   Dim Ans As Variant
   
   Ans = Application.GetOpenFilename
   If Ans = False Then
      MsgBox "Cancelled"
   End If
End Sub

Sub Test3()
   Dim Ans As Variant
   
   Ans = Application.GetOpenFilename(MultiSelect:=True)
   If Ans = False Then
      MsgBox "Cancelled"
   End If
End Sub

In all of them, try clicking on 'Cancel' or selecting a file (or various files, in Test3)
 
Upvote 0

Forum statistics

Threads
1,203,620
Messages
6,056,330
Members
444,861
Latest member
B4you_Andrea

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