Automate user selected files.

Shakeable_Drip

Board Regular
Joined
May 30, 2023
Messages
50
Office Version
  1. 365
Platform
  1. Windows
My macro directs the users to select .txt files for import.
At the moment, a file explorer window pops up and users have to paste the folder directory and select the files, somewhat cumbersome.
A copy of the workbook is always in the directory with a folder named Temp where the .TXT files are located.
Can I do 1 one 2 things:
1. file explorer opens to this workbook's location.
2. (my preference) instead of using a pop up can excel grab all the .TXT files at *this workbook's location/temp

Thanks for the help!

VBA Code:
Sub TXT_Import()
  Dim DATA As Variant
  Dim sourceWorkbook As Workbook
  Dim sourceRange As Range
  Dim destinationWorksheet As Worksheet
  Dim nextColumn As Long, i As Long
 
  DATA = Application.GetOpenFilename("TXT-DATA (*.txt), *.txt", MultiSelect:=True)
  If Not IsArray(DATA) Then Exit Sub
  Application.ScreenUpdating = False
 
  Set destinationWorksheet = ThisWorkbook.Sheets("IMPORT")
  nextColumn = 1
  For i = LBound(DATA) To UBound(DATA)
    Set sourceWorkbook = Workbooks.Open(DATA(i), local:=True)
    With sourceWorkbook.ActiveSheet
      Set sourceRange = .UsedRange.Resize(.UsedRange.Rows.Count - 1).Offset(1, 0)
    End With
    sourceRange.Copy destinationWorksheet.Cells(1, nextColumn)
    nextColumn = nextColumn + sourceRange.Columns.Count
    sourceWorkbook.Close False
  Next i
 
  Application.ScreenUpdating = True
  'MsgBox "Completed . . .", vbInformation 'optional
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Well I solved 1. after realizing that an old document name was reference as "TXT-DATA"
But can I eliminate the need to manually select the files when the temp folder is always there containing the .TXT files?
 
Upvote 0

Forum statistics

Threads
1,215,089
Messages
6,123,058
Members
449,091
Latest member
ikke

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