steve400243
Active Member
- Joined
- Sep 15, 2016
- Messages
- 429
- Office Version
- 365
- 2016
- Platform
- Windows
Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several problems when trying to run it. Was hoping someone could look at it and see any errors or suggestions they may have to make it run better? thanks for any help provided.
Code:
Sub t()
Dim wb As Workbook, sh As Worksheet, ary As Variant, fPath As String, fName As String, i As Long, rw As Long
Application.ScreenUpdating = False
fPath = "X:\SEA Shares\warehouse\CFS and FMM Program\SEA Devanned February-2020\"
Set sh = ThisWorkbook.Sheets("Sheet1")
ary = Array("C3", "C4", "C5", "H2", "H3", "H4")
fName = Dir(fPath & "*.xls*")
Do While fName <> ""
Application.StatusBar = "Please be patient... processing: " & fName
If fName <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(fPath & fName)
'Header (Optional)
rw = LastRow(sh.Range("B60000:M60000")) + 1
ThisWorkbook.Sheets("Param").Range("B1:M3").Copy sh.Cells(rw, 2)
'Data 1
rw = LastRow(sh.Range("B60000:M60000")) + 1
For i = 2 To 7
sh.Cells(rw, i) = wb.Sheets(1).Range(ary(i - 2)).Value
Next i
'Data 2 (Paste Values only)
With wb.Sheets(1)
.Range("A13:A35").Copy
sh.Cells(rw, 8).PasteSpecial xlPasteValues
.Range("G13:G35").Copy
sh.Cells(rw, 9).PasteSpecial xlPasteValues
.Range("H13:H35").Copy
sh.Cells(rw, 10).PasteSpecial xlPasteValues
.Range("J13:J35").Copy
sh.Cells(rw, 11).PasteSpecial xlPasteValues
.Range("K13:K35").Copy
sh.Cells(rw, 12).PasteSpecial xlPasteValues
.Range("D13:D35").Copy
sh.Cells(rw, 13).PasteSpecial xlPasteValues
End With
wb.Close False
End If
fName = Dir
Loop
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
'Find last row when looking at multiple columns. Return 99999 = error.
Function LastRow(rg As Range) As Long
Dim c As Range
If rg.Cells.Count > 100 Then LastRow = 99999: Exit Function
LastRow = 0
For Each c In rg
If c.End(xlUp).Row > LastRow Then
LastRow = c.End(xlUp).Row
End If
Next c
End Function