Combining sheets together using VBA

Rate

Instructions to combine Excel sheets with the same columns into one using VBA

VBA Code

				
					Sub Combine()
    Dim i As Integer
    Dim headerCount As Variant
    Dim rsRow As Worksheet
    On Error Resume Next
LInput:
    headerCount = Application.InputBox("Điền số thứ tự của dòng tên cột", "", "1")
    If TypeName(headerCount) = "Boolean" Then Exit Sub
    If Not IsNumeric(headerCount) Then
        MsgBox "Chỉ được nhập số", , "Msgbox"
        GoTo LInput
    End If
    Set rsRow = ActiveWorkbook.Worksheets.Add(Sheets(1))
    rsRow.Name = "Combined"
    Worksheets(2).Range("A1").EntireRow.Copy Destination:=rsRow.Range("A1")
    For i = 2 To Worksheets.Count
        Worksheets(i).Range("A1").CurrentRegion.Offset(CInt(headerCount), 0).Copy _
               Destination:=rsRow.Cells(rsRow.UsedRange.Cells(rsRow.UsedRange.Count).Row + 1, 1)
    Next
End Sub
				
			

Hướng dẫn sử dụng

Use the keyboard shortcut ALT + F11 to open the VBA window in Excel.

Then create a new Module, paste the above code in.

Execute the code with the F5 key.

After running, enter the serial number of the row containing the column name, usually 1.

Sheet “Combined” will be created immediately after containing the results.

Above is an article on how to combine Excel sheets with the same columns using VBA.

Wish you success in applying to work or study!

Leave a Reply