VBA to Open an excel file using Inputs

bellg

New Member
Joined
May 13, 2014
Messages
29
I'm trying to get this code to open a file for me:

Sub openfile()


Dim A As sting
Dim B As String


A = Application.InputBox("type value of A here:")
B = Application.InputBox("type value of B here:")


Workbooks.Open Filename:= _
"D:\Users\" & A & "\Documents\" & B & ".xlsx"

End Sub


It's not working and I'm not sure why. Can anyone help me?

Thanks,
Greg
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Just a quick glance...

Code:
Dim A As sting

Try String instead of sting...

Cheers,

tonyyy
 
Upvote 0
Another approach to opening a file...

Code:
Sub openfile()
Dim FilePath As String
FilePath = Application.GetOpenFilename
Workbooks.Open Filename:=FilePath
End Sub
 
Upvote 0
Sorry for the typo. That isn't my problem, however.

Thanks for the second option.

I'm still really looking for why my first code doesn't work. More specifically, this part:

Workbooks.Open Filename:= _
"D:\Users\" & A & "\Documents\" & B & ".xlsx"

I can type in the file name manually without the concatenation, but that's not what I want. I want the user to be prompted to type in something specific and use that in the filepath.

Thanks, Greg
 
Last edited:
Upvote 0
Greg,

Your code works in my Excel 2010 / Windows 7 environment, albeit I used "C:\Users\" rather than "D:\Users\".

Are you getting an error message?
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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