때로는 데이터를 쉽게 분석하고 유용한 정보로 변환 할 수 있도록 여러 시트를 하나의 시트로 병합하려고합니다. 이 기사에서는 VBA를 사용하여 여러 워크 시트를 하나의 워크 시트로 병합하는 방법을 설명합니다.

예 :

008

여기에서는 데이터를 다른 워크 시트로 반환하는 서버에서 일부 데이터를 가져 왔습니다. 시트를 하나 더 추가하고 이름을 “마스터”로 지정했습니다. 다른 시트 이름은 중요하지 않습니다.

이제이 매크로를 실행하십시오.

Sub Merge_Sheets()

Dim startRow, startCol, lastRow, lastCol As Long

Dim headers As Range

'Set Master sheet for consolidation

Set mtr = Worksheets("Master")

Set wb = ThisWorkbook

'Get Headers

Set headers = Application.InputBox("Select the Headers", Type:=8)

'Copy Headers into master

headers.Copy mtr.Range("A1")

startRow = headers.Row + 1

startCol = headers.Column

Debug.Print startRow, startCol

'loop through all sheets

For Each ws In wb.Worksheets

'except the master sheet from looping

If ws.Name <> "Master" Then

ws.Activate

lastRow = Cells(Rows.Count, startCol).End(xlUp).Row

lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column

'get data from each worksheet and copy it into Master sheet

Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _

mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)

End If

Next ws

Worksheets("Master").Activate

End Sub

이 VBA 매크로를 사용하여 시트를 병합하는 방법은 무엇입니까?

  1. 새 시트를 삽입하고 통합 문서에서 “마스터”라는 이름을 지정합니다. 원하는 경우 나중에 이름을 변경하십시오.

  2. VBA 편집기에 모듈을 삽입하고 VBA 코드 위에 복사하십시오.

  3. 매크로를 실행하십시오.

  4. 제목을 선택하라는 메시지가 표시됩니다. 제목을 선택하고 확인을 누르십시오.

그리고 끝났습니다. 모든 시트가 마스터에 병합됩니다.

0012

어떻게 작동합니까?

VBA에서 개체 및 변수 생성의 기본 사항을 알고 있다고 가정합니다. 첫 번째 부분에서는 작업에 필요한 개체와 변수를 만들었습니다.

내가 vba 코드에서 주석을 사용하여 설명했던 대부분의 것.

이 vba 코드의 주요 부분을 살펴 보겠습니다.

For Each ws In wb.Worksheets

'except the master sheet from looping

If ws.Name <> "Master" Then

ws.Activate

lastRow = Cells(Rows.Count, startCol).End(xlUp).Row

lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column

'get data from each worksheet and copy it into Master sheet

Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _

mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)

End If

Next ws

이전 기사에서 시트를 반복하는 방법과 vba를 사용하여 마지막 행과 열을 가져 오는 방법을 배웠습니다.

여기에서는 for 루프를 사용하여 기본 통합 문서의 각 시트를 반복합니다.

wb.Worksheets의 각 ws 그런 다음 해당 시트의 데이터를 통합 할 것이므로 “마스터”시트를 루핑에서 제외합니다.

그런 다음 마지막 행과 마지막 열 번호를 얻습니다.

이제 다음 줄이 매우 중요합니다. 한 줄로 여러 작업을 수행했습니다.

Range (Cells (startRow, startCol), Cells (lastRow, lastCol)). Copy _ mtr.Range ( “A”& mtr.Cells (Rows.Count, 1) .End (xlUp) .Row + 1)

먼저 startRow, startCol, lastRow 및 lastCol을 사용하여 범위를 형성합니다.

Range(Cells(startRow, startCol), Cells(lastRow, lastCol))

We have copied it using copy method of range.

Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy

We pasted it directly into first blank cell after last non blank cell in column A of master sheet (mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1).

Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _

mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)

이 루프는 모든 시트에 대해 실행되고 각 시트 데이터를 마스터 시트에 복사합니다.

마지막으로 매크로 끝에서 마스터 시트를 활성화하여 출력을 확인합니다.

예 여러분, 이것이 통합 문서의 모든 시트를 병합하는 방법입니다. 이 VBA 코드 또는 아래의 주석 섹션에있는 Excel 항목에 대한 질문이 있으면 알려주십시오.

파일 다운로드 :

`link : /wp-content-uploads-2019-11-Consolidate_Merge-multiple-worksheets-into-one-master-sheet-using-VBA.xls [__ Consolidate_VBA를 사용하여 여러 워크 시트를 하나의 마스터 시트로 병합]

관련 기사 :

시트를 반복하는 방법

vba를 사용하여 마지막 행과 열을 얻는 방법

link : / files-workbook-and-worksheets-in-vba-add-and-save-new-workbook-using-vba-in-microsoft-excel [Microsoft Excel 2016에서 VBA를 사용하여 새 통합 문서 추가 및 저장]

link : / menus-toolbars-status-bar-in-vba-display-a-message-on-the-statusbar-using-vba-in-microsoft-excel [Excel VBA 상태 표시 줄에 메시지 표시]

link : / general-topics-in-vba-turn-off-warning-messages-using-vba-in-microsoft-excel [Microsoft Excel 2016에서 VBA를 사용하여 경고 메시지 끄기]

인기 기사 :

link : / formulas-and-functions-introduction-of-vlookup-function [Excel의 VLOOKUP 함수]

link : / tips-countif-in-microsoft-excel [Excel 2016의 COUNTIF]

link : / excel-formula-and-function-excel-sumif-function [Excel에서 SUMIF 함수 사용 방법]