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
 
Okay hitting a roadblock when I select the file it is trying to open in the tab the button is. Even though I am activating the sheet within the code

Sheets("TXT").Activate
Range("A:Z").Select
Selection.ClearContents
Range("A1").Select

OpenCopyTXT (this is a sub of the above, also put it below)

Sub OpenCopyTXT()
Dim Fname As String
With Application.FileDialog(3)
.InitialFileName = "MY PATH"
.Filters.Add "Text Files Only", "*.txt"
If .Show = -1 Then Fname = .SelectedItems(1)
End With
End Sub
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
There is nothing there that opens the file, it only gets the file name.
 
Upvote 0
So i was going to give the user an option to open the file through the dialogbox. My concern with pegging the file name is that the file name will change each day with the time it was saved at the end. Is there a way for me to import the file if a certain criteria is met so if file name is "AAA_20200529_0515" and only care about the is met "AAA_20200529"i guess that is one way i could work around it?
 
Upvote 0
You can use the code to allow the user to select the correct & then use that filename to open it.
Depending on what type of text file it is you might be able to use
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
   Workbooks.OpenText Fname
End Sub
 
Upvote 0
Thank you Fluff so that actually worked. And if i wanted to remove the option to select via a dialog box and force the file to open using a string of variables that are met in the file name is that doable?

AAA_20200529_0515
 
Upvote 0
Yes that's doable, but you will need to start a new thread, as it's a different problem.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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