Code: Select all
Sub MergeSheets()
Dim mainSheet As Worksheet
Dim ws As Worksheet
Dim lastRow As Long
Dim targetRow As Long
Dim i As Long, l As Long
Dim arr(99999, 20) As Variant
On Error Resume Next
Set mainSheet = ThisWorkbook.Worksheets("MainSheet")
On Error GoTo 0
If mainSheet Is Nothing Then
Set mainSheet = ThisWorkbook.Worksheets.Add
mainSheet.Name = "MainSheet"
Else
' mainSheet.Cells.Clear
End If
targetRow = 3
For Each ws In ThisWorkbook.Worksheets
If ws.Index > 1 And ws.Name <> mainSheet.Name Then
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
For i = 18 To lastRow
If ws.Cells(i, "D").Value <> "" And IsNumeric(ws.Cells(i, "D").Value) Then
arr(l, 0) = VBA.Right(ws.Cells(9, "L").Value, 4)
arr(l, 1) = ws.Cells(9, "L").Value
arr(l, 2) = ws.Cells(9, "M").Value
arr(l, 3) = ws.Cells(4, "M").Value
arr(l, 4) = ws.Cells(8, "D").Value
arr(l, 5) = ws.Cells(10, "D").Value
arr(l, 6) = ws.Cells(11, "E").Value
arr(l, 7) = ws.Cells(12, "E").Value
arr(l, 8) = ws.Cells(11, "M").Value
arr(l, 9) = ws.Cells(13, "L").Value
arr(l, 10) = ws.Cells(14, "D").Value
arr(l, 11) = ws.Cells(i, "D").Value
arr(l, 12) = ws.Cells(i, "E").Value
arr(l, 13) = ws.Cells(i, "I").Value
arr(l, 14) = ws.Cells(i, "J").Value
arr(l, 15) = ws.Cells(i, "K").Value
arr(l, 16) = ws.Cells(i, "L").Value
arr(l, 17) = ws.Cells(i, "M").Value
arr(l, 18) = ws.Cells(62, "E").Value
arr(l, 19) = ws.Cells(63, "E").Value
arr(l, 20) = ws.Cells(64, "E").Value
targetRow = targetRow + 1
l = l + 1
End If
Next i
End If
Next ws
If l > 0 Then
With mainSheet
.Cells.ClearContents
.Range("A1:U1").Value = Array("ลำดับ", "P/O No.", "Date", "CAPRE NO :", _
"Shipping Name", "Vendor Name", "Vendor Address", "Vendor Tell", _
"Credit Term:", "Refer P/R No :", "Dept.Order : ", "Item ", "Description", _
"Request Date", "Unit", "Qty", "Unit Price(Baht)", "Amount(Baht)", "Notes:1", "Notes:2", "Notes:3")
.Range("a2").Resize(l, UBound(arr, 2) + 1) = arr
End With
End If
End Sub