VBA - dialog box, open txt and copy/paste in excel

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Have considerable amount of issues trying to get my VBA to even start. in quick summary was looking to:

1) A button to allow user a dialog box to select a .txt file (defaulting the folder path)
2) Clear contents of a tab in an existing workbook where the .txt file will be copied and paste it into
3) Formatting the data to remove any blanks rows between the data

I am not overly concerned with 2 & 3. #1 is where i am struggling. every time i use msoFileDialogFilePicker or msoFileDialogOpen it is giving me a compile error.

Dim intChoice As Integer

Application.FileDialog(msoFileDialogOpen).InitialFileName = "MY PATH"
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _"Text Files Only", "*.txt")
End If
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
VBA Code:
Sub ItalianPlatinum()
   Dim Fname As String
   With Application.FileDialog(3)
      .InitialFileName = "C:\mrexcel\text & csv\"
      .Filters.Add "Text Files Only", "*.txt"
      If .Show = -1 Then Fname = .SelectedItems(1)
   End With
End Sub
 
Upvote 0
Do you want the user to select a file or open a file?
 
Upvote 0
How about
VBA Code:
Sub ItalianPlatinum()
   Dim Fname As String
   With Application.FileDialog(3)
      .InitialFileName = "C:\mrexcel\text & csv\"
      .Filters.Add "Text Files Only", "*.txt"
      If .Show = -1 Then Fname = .SelectedItems(1)
   End With
End Sub
How do I get it to default to a folder path on the dialogbox?
 
Upvote 0
Just change this to your path
Rich (BB code):
.InitialFileName = "C:\mrexcel\text & csv\"
 
Upvote 0
That suggests that the path name you used is not valid.
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,944
Members
449,198
Latest member
MhammadishaqKhan

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