VBA - File Dialog Show Result in Textbox on userform

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I cannot find how to change this code so that the file path of the selected files are place in an existing text box (TextBox3) in my userform.

Here is the code I'm using

Code:
Private Sub CommandButton1_Click()
Dim fDialog As FileDialog, i As Integer
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
fDialog.AllowMultiSelect = True
fDialog.Title = "Select Files"
fDialog.InitialFileName = "T:\Finance\SohoMaccs\2018Maccs\Monthly Reports\"
fDialog.Filters.Add "Excel files", "*.xlsx"
If fDialog.Show = -1 Then
    For Each it In fDialog.SelectedItems
    Debug.Print it
    Next it
End If

End Sub

The above code works until files are selected. Thereafter nothing happens.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Do you need this?

Code:
Private Sub CommandButton1_Click()
    Dim fDialog As FileDialog, i As Integer
    Dim n As Integer
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fDialog.AllowMultiSelect = True
    fDialog.Title = "Select Files"
    fDialog.InitialFileName = "T:\Finance\SohoMaccs\2018Maccs\Monthly Reports\"
    fDialog.Filters.Add "Excel files", "*.xlsx"
    If fDialog.Show = -1 Then
        For Each it In fDialog.SelectedItems
            Debug.Print it
        Next it
        n = InStrRev(fDialog.SelectedItems(1), "\")
        TextBox3.Value = Left(fDialog.SelectedItems(1), n)
    End If
End Sub

Regards Dante Amor
 
Upvote 0
Thank you Dante.

When I run this code I get 'Object required' error. The highlighted code is:

TextBox3.Value = Left(fDialog.SelectedItems(1), n)
 
Upvote 0
You commented that you had a textbox3, you must have a textbox called textbox3 before executing the code.
 
Upvote 0
You commented that you had a textbox3, you must have a textbox called textbox3 before executing the code.


I do have a textbox called TextBox3. It is in my userform which is where I want the list to be.
 
Upvote 0
try this
Code:
Private Sub CommandButton1_Click()
    Dim fDialog As FileDialog, i As Integer
    Dim n As Integer
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fDialog.AllowMultiSelect = True
    fDialog.Title = "Select Files"
    fDialog.InitialFileName = "T:\Finance\SohoMaccs\2018Maccs\Monthly Reports\"
    fDialog.Filters.Add "Excel files", "*.xlsx"
    If fDialog.Show = -1 Then
        For Each it In fDialog.SelectedItems
            Debug.Print it
        Next it
        n = InStrRev(fDialog.SelectedItems(1), "\")
        msgbox Left(fDialog.SelectedItems(1), n)
    End If
End Sub
 
Upvote 0
Thanks. This time it shows the correct result in a message box that opens. I want it to show the result in my userform in the textbox I have in there.
 
Upvote 0
Is the command button that is running the code on the userform?
 
Upvote 0
But you do not have a textbox called textbox3, so it sends the error message. You just have to put the name of some textbox that you have in your userform.
TextBox3.Value = Left(fDialog.SelectedItems(1), n)
 
Upvote 0
Yes it is. Here is the comandbutton code:

Code:
Private Sub CommandButton1_Click()
Application.Run "Module2.GetWorksheetListing"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,108
Members
449,205
Latest member
ralemanygarcia

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