|
'
'-------------------------------------------------------------
'-------------------------------------------------------------
'-------------------------------------------------------------
Sub un_pivot_data()
'-------------------------------------------------------------
Dim rngX As Range
Set rngX = Worksheets(1).Range("A1").CurrentRegion
' 상품코드 칼라 사이즈 수량 매출
Dim iUnitPrice As Variant
Dim row As Range
Dim colx As New Collection
Dim unitX As Variant
Dim cell As Range
'---------------------------------------------------
For r = 2 To rngX.Rows.Count
'---------------------------------------------------
Set row = rngX.Rows.Item(r)
' 총판매금액이 0이면 스킵, 즉 아래 코드를 실행하지 않고
' for의 젤 끝이로 이동
If row.Cells(3).Value = "" Or row.Cells(3).Value = 0 Then GoTo end_line
' 상품 단가 구하기
iUnitPrice = Int(row.Cells(3).Value / row.Cells(4).Value)
' 템플릿 배열
unitX = Array(row.Cells(1), row.Cells(2).Value, "", "", "")
' 범위 중 상수인 것만 선택하여 각 셀마다 루핑
'----------------------------------------------------------
For Each cell In row.Cells(5).Resize(1, 19).SpecialCells(2)
'----------------------------------------------------------
'배열에 새로운 값 채우기
' 상품코드 칼라 사이즈 수량 매출
unitX(3) = cell.Value
unitX(4) = iUnitPrice * cell.Value
' size
unitX(2) = CStr(rngX.Cells(1, cell.Column).Value)
' collection에 집어넣기
colx.Add unitX
Next cell
end_line:
Next
' Stop
' 데이타 뿌리기
'----------------------
' 결과 담을 시트 선언
Dim shtY As Worksheet: Set shtY = Worksheets("result")
' 기존 자료 삭제
shtY.[A2:E20000].ClearContents
' 사이즈 컬럼의 셀 서식을 텍스트로 수정
shtY.Columns(3).NumberFormat = "@"
' Collection의 각 요소를 돌며
For r = 1 To colx.Count
' 컬렉션의 요소는 배열이므로 resize를 하여
' 범위의 크기를 맞춘 후 value 속성에 배열을 넣어주면
' 배열이 셀에 보기좋게 들어간다.
shtY.Range("A" & (r + 1)).Resize(1, 5).Value = colx.Item(r)
Next
'rngX.Sort Key1:=Cells(1, 1), order1:=xlAscending, Header:=xlNo
' Stop
' 상품코드 기준 오름차순 정렬
shtY.[A1].CurrentRegion.Sort Key1:=Cells(1, 1), order1:=xlAscending, Header:=xlYes
End Sub
|
|