Using inputbox to open specific files from a folder

tcox090

New Member
Joined
Sep 10, 2015
Messages
1
As a warning, I am somewhat new to using VBA, butI have a feeling I am pretty close to figuring this out.
I have a folder with a bunch of different files and I need to open them one at a time to compare them with a master file.

My input box is where I will have the user add the specific file name. A
fter doing this, my string is left with quotations around it so when I put data_wbk1 into the file path, I am getting a run-time error because 'C:Users\......\"CapFcst2014".xlsm' could not be found. If i get rid of the quotes around the file name, it would open right up.

'Open Outdated Workbook
data_wbk1 = InputBox("Enter File Name:", Default:="Paste File Name Here")
Workbooks.Open ("C:\Users\036494\Desktop\CapComp_14&15\ """ & data_wbk1 & """.xlsm")

Thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I think you're looking for:

'Open Outdated Workbook
data_wbk1 = InputBox("Enter File Name:", Default:="Paste File Name Here")
Workbooks.Open ("C:\Users\036494\Desktop\CapComp_14&15\" & data_wbk1 & ".xlsm")
 
Upvote 0
Another way to do the same thing but give the user the ability to click the file from a File Explorer window...


Add this to a Module:
Code:
Function GetFileName() As Variant
Dim FInfo As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
 
'Set up list of file filters
FInfo = "All Files (*.*) ,*.*," & _
        "Old Excel (*.xls) , *.xls," & _
        "New Excel (*.xlsx) , *.xlsx," & _
"Macro Excel (*.xlsm) , *.xlsm"
FilterIndex = 3            'Change to select a default filter
Title = "Select a File containing the comment data"
'Get the filename
FileName = Application.GetOpenFilename(FInfo, FilterIndex, Title)
'Handle return info from dialog box
If FileName = False Then
    GetFileName = ""
Else
    GetFileName = FileName
End If
End Function

The above function allows the user to select the file to be used in your code. To use the function, you would add the following to your code.

Code:
Dim FileName As String
 
FileName = GetFileName()
If Len(FileName) > 1 Then
    Workbooks.Open FileName:=FileName, ReadOnly:=True
    Set wb = Workbooks(Dir(FileName))
    'do something with newly opened workbook here
Else
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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