thelukeee
New Member
- Joined
- Sep 8, 2020
- Messages
- 10
- Office Version
- 2019
- Platform
- Windows
Hello everyone,
I'd appreciate some coding help in Excel using VBA. I'm very new to this.
Background: There is a new .txt file made for each machine run, which contains details about the part and the weight gain. The .txt file is created automatically in the system, and goes unedited by anyone, therefore using the date last modified is a viable way to sort through the files.
Goal of project:
1. The purpose of this code is the pull all of the files located the file name textFilesFolder="_____".
2. List out all the lines in the .txt files in separate columns (a single text file would have multiple rows in excel)
3. To be able to only pull files that have a 'lastModifiedDate' that are within a desired range (between 'Start Date' and 'End Date')
This is the purpose of the For Each statement. The contents of the .txt file should separated into separate columns based on the commas in the text file.
Error: "Run-time error '76: Path not Found"
Notes: This is a very similar project to my previous problem: Importing Multiple .Txt Files Into Excel Based on the Date Last Modified (VBA)
*This question is only posted on MrExcel here
Picture #1: Desired format to in excel
Picture #2: Folder that .Txt files are contained in
Picture #3: Example .Txt file
Thank you very much for any help.
I'd appreciate some coding help in Excel using VBA. I'm very new to this.
Background: There is a new .txt file made for each machine run, which contains details about the part and the weight gain. The .txt file is created automatically in the system, and goes unedited by anyone, therefore using the date last modified is a viable way to sort through the files.
Goal of project:
1. The purpose of this code is the pull all of the files located the file name textFilesFolder="_____".
2. List out all the lines in the .txt files in separate columns (a single text file would have multiple rows in excel)
3. To be able to only pull files that have a 'lastModifiedDate' that are within a desired range (between 'Start Date' and 'End Date')
This is the purpose of the For Each statement. The contents of the .txt file should separated into separate columns based on the commas in the text file.
Error: "Run-time error '76: Path not Found"
VBA Code:
Public Sub Import_WeightGain_Text_Files()
Dim TestBaseCell As Range
Dim textFilesFolder As String
Dim startDate As Date, endDate As Date
Dim FSO As Object, FSfile As Object, ts As Object
Dim LineItems As Variant
Dim ws As Worksheet
Dim row_number As Long
Set ws = Sheets("Raw WG Data")
ws.Cells.Clear
textFilesFolder = "\\janedoe\HTM_MACH_WG2\New Weight Gain Storage\ARCH" '*folder that contains my text files*
startDate = Worksheets("Intro").Range("B5").Value
endDate = Worksheets("Intro").Range("B6").Value
Set FSO = CreateObject("Scripting.FileSystemObject")
Application.ScreenUpdating = False
Application.EnableEvents = False
row_number = 2 'to be placed under the column titles
With Worksheets("Raw WG Data")
For Each FSfile In FSO.GetFolder(textFilesFolder).Files
If LCase(FSfile.Name) Like LCase("*.txt") And FSfile.DateLastModified >= startDate And FSfile.DateLastModified <= endDate Then
Open FSfile For Input As #1
Do Until EOF(1)
Line Input #1, LineFromFile
LineItems = Split(LineFromFile, ",")
ActiveCell.Offset(row_number, 1).Value = LineItems(3) 'Date
ActiveCell.Offset(row_number, 2).Value = LineItems(0) 'Lot NBR
ActiveCell.Offset(row_number, 3).Value = LineItems(1) 'J/N
ActiveCell.Offset(row_number, 4).Value = LineItems(2) 'Serial NBR
ActiveCell.Offset(row_number, 5).Value = LineItems(4) 'Pre-Weight (g)
ActiveCell.Offset(row_number, 6).Value = LineItems(5) 'Post-Weight (g)
ActiveCell.Offset(row_number, 7).Value = LineItems(6) 'Weight Diff (g)
row_number = row_number + 1 'Each line from the .txt file has its own row
Loop
Close #1
End If
row_number = row_number + 1 'After the text file is finished, then another row is added to begin copying the data for the next .txt file
Next
End With
End Sub
Notes: This is a very similar project to my previous problem: Importing Multiple .Txt Files Into Excel Based on the Date Last Modified (VBA)
*This question is only posted on MrExcel here
Picture #1: Desired format to in excel
Picture #2: Folder that .Txt files are contained in
Picture #3: Example .Txt file
Thank you very much for any help.