Uploaded by Kyongsok Choe

엑셀 매크로 시트 병합 방법

advertisement
아무 시트나 선택해서 사용가능한 매크로 시트 탭 선택후 오른쪽 클릭해서 View Code
(visual Basic) – 아래의 macro 를 복사해서 붙여넣기 한 뒤
버튼을 누르면 매크로가
실행되어 모든 시트의 데이터가 MyMergeSheet 로 합쳐진다
Sub MergeDataFromWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim erow As Long, lrowsh As Long, startrow As Long
Dim CopyRng As Range
startrow = 2
*데이터 수합하기 시작하는 열 설정
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
‘Delete the sheet “MyMergeSheet”
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets(“MyMergeSheet”).Delete
On Error GoTo 0
Application.DisplayAlerts = True
‘Add a worksheet with the name “MyMergeSheet”
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = “MyMergeSheet”
‘loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
‘Find the next blank or empty row on the DestSh
erow = DestSh.Range(“A” & Rows.Count).End(xlUp).Offset(1, 0).Row
‘Find the last row with data in the Sheet
lrowsh = sh.Range(“A” & Rows.Count).End(xlUp).Row
Set CopyRng = sh.Range(sh.Rows(startrow), sh.Rows(lrowsh))
‘copies values/formats
CopyRng.Copy
With DestSh.Cells(erow, 1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
‘AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
1 번시트에 이어 그뒤 2 번 시트 내용부터 모든 내용이 선택한 시트로 합쳐지는 VBA
Sub SheetUnit()
Dim i As Integer
Dim ShtA As Worksheet
Dim rngB As Range
Set ShtA = Sheets(1)
For i = 2 To Sheets.Count
(<= 저장위치 1 번시트 - 기존 데이터에 이어붙인다)
(<= 2 번시트부터 합치기 시작 – 1 번 시트뒤에)
Set rngB = ShtA.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Sheets(i).UsedRange.Copy rngB
Next i
End Sub
합친 시트를 삽입하고자 하는 시트를 클릭하고 시트이름 오른쯕 클릭 – View Code (visual Basic)
– (Insert – Module) 위 macro 복사해서 붙여넣기 한다
원하는 도형을 삽입해서 right click – Macro Assign – macro ‘sub sheetunit()’ 선택해서 enter –
만들어 놓은 도형 클릭하면 매크로가 실행되어 1 번시트에 이어 그뒤 2 번 시트 내용부터 모든
내용이 선택한 시트로 합쳐진다
Download