이 기사에서는 지정된 시트에 여러 영역을 결합하는 매크로를 만듭니다.

원시 데이터는 이름 및 연령을 포함하는 일부 샘플 데이터로 구성됩니다. 원시 데이터를 포함하는 두 영역이 있습니다. 두 영역을 “대상”시트에 통합하려고합니다.

ArrowMain

“레코드 복사”버튼을 클릭하면 서식과 함께 두 영역의 데이터가 결합됩니다.

ArrowOutputRecord

“값만 복사”버튼을 클릭하면 두 영역의 데이터가 합쳐 지지만 셀 형식은 복사되지 않습니다.

ArrowOutputValuesOnly

코드 설명

Sheets ( “Main”). Range ( “A9 : B13, D16 : E20”). Areas Next Smallrng의 각 Smallrng에 대해 위의 For Each 루프는 정의 된 영역을 반복하는 데 사용됩니다.

Set DestRange = Sheets ( “Destination”). Range ( “A”& LastRow)

위의 코드는 데이터를 복사하려는 마지막 셀의 범위 개체를 만드는 데 사용됩니다.

Smallrng.Copy DestRange 위 코드는 지정된 대상에 데이터를 복사하는 데 사용됩니다.

아래 코드를 따르세요

Option Explicit

Sub CopyMultiArea()

'Declaring variables

Dim DestRange As Range

Dim Smallrng As Range

Dim LastRow As Long

'Looping through specified areas

For Each Smallrng In Sheets("Main").Range("A9:B13,D16:E20").Areas



'Finding the row number of last cell

LastRow = Sheets("Destination").Range("A1").SpecialCells(xlLastCell).Row + 1



'Selecting the cell where records need to be copy

If LastRow = 2 Then

Set DestRange = Sheets("Destination").Range("A" & LastRow - 1)

Else

Set DestRange = Sheets("Destination").Range("A" & LastRow)

End If



'Copying records to specified destination range

Smallrng.Copy DestRange



Next Smallrng

End Sub

Sub CopyMultiAreaValues()

'Declaring variables

Dim DestRange As Range

Dim Smallrng As Range

Dim LastRow As Long

'Looping through specified areas

For Each Smallrng In Sheets("Main").Range("A9:B13,D16:E20").Areas



'Finding the row number of last cell

LastRow = Sheets("Destination").Range("A1").SpecialCells(xlLastCell).Row + 1



With Smallrng

'Selecting the cell where records need to be copy

If LastRow = 2 Then

Set DestRange = Sheets("Destination").Range("A" & LastRow - 1).Resize(.Rows.Count, .Columns.Count)

Else

Set DestRange = Sheets("Destination").Range("A" & LastRow).Resize(.Rows.Count, .Columns.Count)

End If

End With



'Assigning the values from source to destination

DestRange.Value = Smallrng.Value



Next Smallrng

End Sub

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

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