때로는 동일한 열의 여러 시트에서 데이터를 복사하는 것이 일상적인 작업이됩니다. 이 단계는 자동화를 사용하여 피할 수 있습니다. 각 시트의 열에서 데이터를 하나의 시트로 복사 한 후 통합 시트를 만들고 싶다면이 기사를 읽어야합니다.

이 기사에서는 특정 열의 데이터를 복사하여 새 시트에 붙여 넣는 매크로를 만듭니다.

이 예의 원시 데이터는 직원의 부서별, 개인 및 연락처 세부 정보가 포함 된 세 개의 시트가 포함 된 Excel 통합 문서 형식의 직원 데이터로 구성됩니다.

ArrowRaw

다른 시트의 데이터를 새 시트로 복사하기 위해 “CopyColumns”매크로를 만들었습니다. 이 매크로는 “메인”시트에서 “매크로 실행”버튼을 클릭하여 실행할 수 있습니다.

ArrowMain

“CopyColumns”매크로는 “Main”시트 뒤에 “Master”라는 새 시트를 삽입합니다. “마스터”시트에는 모든 시트의 통합 데이터가 포함됩니다.

ArrowOutput

코드 설명

Worksheets.Add (after : = Worksheets ( “Main”))

위의 코드는 “기본”워크 시트 뒤에 새 워크 시트를 삽입하는 데 사용됩니다.

If Source.Name <> “Master”And Source.Name <> “Main”Then End If Above 코드는 “마스터”및 “메인”시트의 데이터 복사를 제한하는 데 사용됩니다.

Source.UsedRange.Copy Destination.Columns (Last)

위의 코드는 원본 시트에서 대상 시트로 데이터를 복사하는 데 사용됩니다.

ThisWorkbook.Worksheets의 각 원본에 대해 Source.Name = “Master”Then MsgBox “마스터 시트가 이미 있습니다.”

다음 경우 Sub End 종료 위 코드는 통합 문서에 “Master”시트가 이미 있는지 확인하는 데 사용됩니다. 통합 문서에 “마스터”시트가 이미 있으면 매크로 실행이 중지됩니다.

아래 코드를 따르세요

Option Explicit

Sub CopyColumns()

Dim Source As Worksheet

Dim Destination As Worksheet

Dim Last As Long

Application.ScreenUpdating = False

'Checking whether "Master" sheet already exists in the workbook

For Each Source In ThisWorkbook.Worksheets

If Source.Name = "Master" Then

MsgBox "Master sheet already exist"

Exit Sub

End If

Next

'Inserting new worksheets in the workbook

Set Destination = Worksheets.Add(after:=Worksheets("Main"))

'Renaming the worksheet

Destination.Name = "Master"

'Looping through the worksheets in the workbook

For Each Source In ThisWorkbook.Worksheets





If Source.Name <> "Master" And Source.Name <> "Main" Then



'Finding the last column from the destination sheet

Last = Destination.Range("A1").SpecialCells(xlCellTypeLastCell).Column



If Last = 1 Then

'Pasting the data in the destination sheet

Source.UsedRange.Copy Destination.Columns(Last)

Else

Source.UsedRange.Copy Destination.Columns(Last + 1)

End If

End If

Next

Columns.AutoFit

Application.ScreenUpdating = True

End Sub

이 블로그가 마음에 들면 Facebook 및 Facebook에서 친구들과 공유하십시오.

여러분의 의견을 듣고 싶습니다. 작업을 개선하고 더 나은 서비스를 제공 할 수있는 방법을 알려주십시오. [email protected]로 문의 해주세요