File open with textbox entry.

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have the code below that opens up a file based on based ona number entered into a textbox in a userform (named frmLOAD).
It works as is but I really need for the user to be able totype what they need to open in the textbox and a directory box pop upprefilled with what was keyed in the textbox so the user is making sure theyare opening the correct file.

Code:
[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub CommandButton1_Click()[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Dim MyFileDir As String, FName As String, myXLFile As String[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]FirstTime As Range[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]

[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]FName = frmLOAD.Textbox1.text[/COLOR][/SIZE][/FONT]

[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]MyFileDir = "P:\Production\Hours ReportingArchives\"[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]myXLFile = FName & "*"[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Workbooks.OpenText Filename:=MyFileDir & myXLFile[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]

 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I have the code below that opens up a file based on based ona number entered into a textbox in a userform (named frmLOAD).
It works as is but I really need for the user to be able totype what they need to open in the textbox and a directory box pop upprefilled with what was keyed in the textbox so the user is making sure theyare opening the correct file.


Try this..

Code:
Sub File_Picker()
Dim MyFileDir As String, FName As String, myXLFile As String, FirstTime As Range
    MyFileDir = "P:\Production\Hours ReportingArchives\"
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .InitialFileName = MyFileDir & frmLOAD.Textbox1.Text
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb"
        .Show
        Verified_Filename = .SelectedItems(i)
    End With
    Workbooks.OpenText Filename:=Verified_Filename
End Sub
 
Last edited:
Upvote 0
Thanks Steve for the reply.
I have two issues.


  1. The file path directory is not automaticallyopening. It is starting at the desktop.

  2. When the user clicks the cancel or “X” button toclose, I get an Run-Time Error
    1. Run-time error ‘5’: Invalid procedure call or argument
 
Upvote 0
Thanks Steve for the reply.
I have two issues.


  1. The file path directory is not automaticallyopening. It is starting at the desktop.
  2. When the user clicks the cancel or “X” button toclose, I get an Run-Time Error
    1. Run-time error ‘5’: Invalid procedure call or argument


Try this instead..


Code:
Sub File_Picker()
Dim MyFileDir As String, FName As String, myXLFile As String, FirstTime As Range
    MyFileDir = "P:\Production\Hours ReportingArchives"
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .InitialFileName = MyFileDir & Userform1.Textbox1.Text
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb"
        .Show
        If .SelectedItems.Count > 0 Then
            Verified_Filename = .SelectedItems(1)
        End If
    End With
    If Not IsEmpty(Verified_Filename) Then
        Workbooks.OpenText Filename:=Verified_Filename
    End If
End Sub
 
Upvote 0
Is there a way to open or select the file without having theuser type the whole thing out?
For instance. If Ihave a file named “1234567 – 543210” couldthe code find it the file by just typing 1234567 in the textbox?

 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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