My Macro should merge all xlsx files from a directory and add a column at the end of files merged, with filename where they come from, trunked.
What's wrong with what I wrote?
Private Sub CommandButton1_Click()
Const Path As String = "MyPath"
Dim FileName As String
Dim WS As Worksheet
Dim WS2 As Worksheet
Dim Lastrow As Long
Dim LastRow2 As Long
Set WS2 = ThisWorkbook.Worksheets(1)
FileName = Dir(Path & "\*.xlsx", vbNormal)
Application.ScreenUpdating = False
Do Until FileName = ""
If FileName <> ThisWorkbook.Name Then
Set WS = Workbooks.Open(Path & "\" & FileName).Sheets("Foglio1")
Lastrow = WS.Cells(500, 1).End(xlUp).Row
WS.Range("C1").Resize(Lastrow, 7).Copy WS2.Cells(LastRow2 + 1, 1)
LastRow2 = WS2.Cells(WS2.Rows.Count, 1).End(xlUp).Row
Cells(Lastrow, LastRow2 + 1).Valure = Left("FileName", 6)
WS.Parent.Close 0
End If
FileName = Dir()
Loop
Application.ScreenUpdating = True
End Sub
It gives me error in the underlined line..
Thanks for help
Bookmarks