Open up all .txt files and import into Excel workbook off partial conditions

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
857
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking for a way to open up .txt files and move them to an excel workbook to then format it to the way I want to so its legible. The issue I am running into is the files naming conventions are very unique. So I don't know how to call the VBA to open and loop through each file. For example:

CONTROL_FUNNA_US_D_20240408182134
CONTROL_FOFNA_US_D_20240408182052

Each file will have multiple occurrences and are saved with the Year month date hour minute and seconds at the end (YYYYMMDDHHMMSS). But for the sake of what I am trying to do I just need one by one open inserted into its respective sheet and formatted. With the file name next to the record that it came from. I am sure there is an easy way to do it but not sure I can currently find a way

FUN has its own sheet
FOF has is own sheet

Appreciate you taking the time to look as always :)
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Take a look here, which shows you how to loop through all the files in a folder with a specific partial value in the file name:
 
Upvote 0
Just want to share just in case anyone else would find use. I added additional components to capture it to pull based off todays date in the file name and create sPartial just for ease of reviewing.

VBA Code:
Option Explicit
Sub Summary()
Dim fName As String, fPath As String, sPartial As String, wb As Workbook

fPath = "\\XXX\XXXX\XXX\XXXX\XXXX\XXXXXX" 'substitute actual path is not in same directory as host workbook.
sPartial = "CONTROL_FUNNA_US_D_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"

If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
fName = Dir(fPath & sPartial)
    Do While fName <> ""
        Set wb = Workbooks.Open(fPath & fName)
        'enter yet to be written code here
        wb.Close 'True or False to either save or not save workbook after code runs.
        fName = Dir
    Loop

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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