ItalianPlatinum
Well-known Member
- Joined
- Mar 23, 2017
- Messages
- 857
- Office Version
- 365
- 2019
- Platform
- Windows
May someone help me I think I am inherently doing something incorrectly. I just want to copy "NonF" down for each file that is transferred over as well as the file name that it came from. But what I have isn't doing that. It is just putting it on the last row.
VBA Code:
Sub Summary()
Dim fName As String, fPath As String, fPartial As String
Dim WsNonF As Worksheet, WsSummary As Worksheet
Dim NrNonF As Long, NrFOF As Long
Dim lastrow As Long, lr As Long, lrr As Long
Dim WbkName As String
Application.ScreenUpdating = False
'set sheet variables
Set WsNonF = Sheets("NONFDATA")
Set WsSummary = Sheets("Summary")
WsNonF.Cells.Delete
WsSummary.Cells.ClearContents
'Non F
fPath = "MY PATH" 'substitute actual path is not in same directory as host workbook.
fPartial = "PARTIAL FINAL NAME" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now) - 1) = 1, "0" & Day(Now) - 1, Day(Now) - 1) & "*.txt"
If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
fName = Dir(fPath & fPartial)
Do While fName <> "" 'open each file loop until no more files to open
Workbooks.OpenText fPath & fName 'opening the txt file
NrNonF = WsNonF.Range("A" & Rows.Count).End(xlUp).Row + 1 'find open row
lastrow = Cells(Rows.Count, "A").End(xlUp).Row - 1 'find the maximum row
WbkName = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1) 'get activeworkbooks name
Range("A2:A" & lastrow).Copy 'Copy data
WsNonF.Range("A" & NrNonF).PasteSpecial Paste:=xlPasteValues 'paste to next open row
lrr = WsNonF.Cells(Rows.Count, "A").End(xlUp).Row 'find the range that came over from the file
WsNonF.Range("L" & lrr).Value = "NonF" 'set the type of file it came from
WsNonF.Range("M" & lrr).Value = WbkName 'set the file name it came from
Application.CutCopyMode = False
Workbooks(fName).Close SaveChanges:=False 'close file dont save
fName = Dir
Loop