Hello, I have a VBA working perfectly to combine data from multiple sheets (all sheets have same column headers, column B to I). I have extra value in Column A (row count), and cell L11/M11 (total sum). I'm only selecting cell B10 to II0 when I select header when prompted, but I get the full sheet copied in the new sheet, including value in column A/L/M. How can I get value from B to I only? I assume it's the part of highlighted area, but I don't know how to fix that.
Here is the code. Thank you
Here is the code. Thank you
VBA Code:
Sub Combine_WorkSheets()
Dim sRow, sCol, lRow, lCol As Long
Dim hdrs As Range
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Master" Then
Application.DisplayAlerts = False
Worksheets("Master").Delete
Application.DisplayAlerts = True
End If
Next Sheet
Sheets.Add.Name = "Master"
Set mtr = Worksheets("Master")
Set wb = ActiveWorkbook
Set hdrs = Application.InputBox("Select the headers", Type:=8)
hdrs.Copy mtr.Range("A1")
sRow = hdrs.Row + 1
sCol = hdrs.Column
Debug.Print sRow, sCol
For Each ws In wb.Worksheets
If ws.Name <> "Master" Then
ws.Activate
lRow = Cells(Rows.Count, sCol).[B]End(xlUp[/B]).Row
lCol = Cells(sRow, Columns.Count).[B]End(xlToLeft)[/B].Column
Range(Cells(sRow, sCol), Cells(lRow, lCol)).Copy _
mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)
End If
Next ws
Worksheets("Master").Activate
ActiveWindow.Zoom = 115
Sheets("Master").Select
Range("B:I").Select
ActiveWorkbook.Worksheets("Master").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Master").Sort.SortFields.Add2 Key:=Range("B2:B1000") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Master").Sort
.SetRange Range("A2:G1000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
End With
End Sub
Last edited by a moderator: