Sub R3_MACRO()
'Sheet 1 작업
Sheets(1).Select
Call R3_ITME
'Sheet 2 작업
Sheets(2).Select
Call R3_ITME_Sheet4
'Sheet 3 작업
Sheets(3).Select
Call R3_ITME_Sheet2
'Sheet 4 작업
Sheets(4).Select
Call R3_ITME_Sheet3
End Sub
Sub R3_ITME()
Dim r_count1 As Long 'Head
Dim r_count2 As Long 'Item
' Dim r_count3 As Long 'job Order
Dim r3table_1 As Object '
Dim r3table_2 As Object '
' Dim r3table_3 As Object '
Dim onePageCnt As Long '한 page의 라인 수
Dim topPageCnt As Long 'top page의 라인수
Dim firstTopPageCnt As Long 'sub top page는 첫 page일 때만 출력
Dim lastBottomPageCnt As Long '마지막 page 시 사용하는 바닥글
Dim bottomPageCnt As Long '바닥글 라인 수
Dim totalPageCnt As Long '토탈 페이지수
'Dim last_itemEndLine As Long 'item의 마지막 라인
'Dim last_LastStaLine As Long '마지막 page 시 사용하는 바닥글 시작 라인
'Dim last_LastEndLine As Long '마지막 page 시 사용하는 바닥글 마지막 라인
Set r3table_1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table
Set r3table_2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table
' Set r3table_3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table
r_count1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table.RowCount
r_count2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table.RowCount
' r_count3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table.RowCount
' 0. (중복으로 출력할) 해더&바텀 DATA 미리 입력 하기
ThisWorkbook.Sheets(1).Cells(6, 2) = r3table_1.Value(1, 1) 'Order From
ThisWorkbook.Sheets(1).Cells(6, 6) = r3table_1.Value(1, 2) 'Deliver To
ThisWorkbook.Sheets(1).Cells(7, 11) = r3table_1.Value(1, 3) 'PO
ThisWorkbook.Sheets(1).Cells(8, 11) = r3table_1.Value(1, 4) 'Date
'ThisWorkbook.Sheets(1).Cells(7, 10) = r3table_1.Value(1, 5) 'JO
ThisWorkbook.Sheets(1).Cells(10, 2) = r3table_1.Value(1, 26) 'GST Registration No.
ThisWorkbook.Sheets(1).Cells(10, 4) = r3table_1.Value(1, 6) 'GST Registration No.
ThisWorkbook.Sheets(1).Cells(11, 3) = r3table_1.Value(1, 24) 'Attn
ThisWorkbook.Sheets(1).Cells(12, 3) = r3table_1.Value(1, 7) 'Phone
ThisWorkbook.Sheets(1).Cells(13, 3) = r3table_1.Value(1, 8) 'Email
ThisWorkbook.Sheets(1).Cells(11, 11) = r3table_1.Value(1, 9) 'Service Client
ThisWorkbook.Sheets(1).Cells(12, 11) = r3table_1.Value(1, 10) 'Client Reference1
ThisWorkbook.Sheets(1).Cells(13, 11) = r3table_1.Value(1, 11) 'Client Reference2
ThisWorkbook.Sheets(1).Cells(14, 11) = r3table_1.Value(1, 12) 'Client Reference3
ThisWorkbook.Sheets(1).Cells(17, 2) = r3table_1.Value(1, 13) 'Delivery Date
ThisWorkbook.Sheets(1).Cells(17, 5) = r3table_1.Value(1, 14) 'Payment Term
ThisWorkbook.Sheets(1).Cells(17, 7) = r3table_1.Value(1, 15) 'Currency
ThisWorkbook.Sheets(1).Cells(17, 9) = r3table_1.Value(1, 16) 'Exchange Rate
ThisWorkbook.Sheets(1).Cells(49, 4) = r3table_1.Value(1, 17) 'Workflow Status
ThisWorkbook.Sheets(1).Cells(51, 4) = r3table_1.Value(1, 18) 'Requested by
ThisWorkbook.Sheets(1).Cells(52, 4) = r3table_1.Value(1, 19) 'Approved by
' Total
ThisWorkbook.Sheets(1).Cells(48, 13) = r3table_1.Value(1, 20) 'Lines subtotal
ThisWorkbook.Sheets(1).Cells(49, 13) = r3table_1.Value(1, 21) 'Misc. Charge Subtotal
ThisWorkbook.Sheets(1).Cells(50, 13) = r3table_1.Value(1, 25) 'Add GST
ThisWorkbook.Sheets(1).Cells(51, 12) = r3table_1.Value(1, 15) 'Currency
ThisWorkbook.Sheets(1).Cells(51, 13) = r3table_1.Value(1, 22) 'Total
' SK Adress 바닥들
' ThisWorkbook.Sheets(1).Cells(48, 1) = r3table_1.Value(1, 23) 'Detail
' ThisWorkbook.Sheets(1).Cells(49, 1) = r3table_1.Value(1, 24) 'Detail
' ThisWorkbook.Sheets(1).Cells(50, 1) = r3table_1.Value(1, 25) 'Detail
' 0. Area 라인
onePageCnt = 62 ' 한 page의 라인 수
topPageCnt = 10 'top page의 라인수
firstTopPageCnt = 8 ' sub top page는 첫 page일 때만 출력
lastBottomPageCnt = 6 '마지막 page 시 사용하는 바닥글
bottomPageCnt = 9 '바닥글 라인 수
dispCell = "M" '출력마지막 CELL
If r3table_1.Value(1, 26) <> "" Then 'GST Registration No.
Range("D10").Select
Selection.HorizontalAlignment = xlLeft
Selection.VerticalAlignment = xlCenter
End If
' 1. 총 페이지수를 계산 하기
'r_count2 = 52 'item의 라인수
'1. 첫페이지만 있을 경우의 item 가능 라인수
lv_cnt_1 = onePageCnt - topPageCnt - firstTopPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'2. 첫페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_2 = onePageCnt - topPageCnt - firstTopPageCnt - bottomPageCnt - 1
'3. 2번째 페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_3 = onePageCnt - topPageCnt - bottomPageCnt - 1
'4. 2번째 페이지가 마지막 페이지 일 경우 item 가능 라인수
lv_cnt_4 = onePageCnt - topPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'--- 1. 1페이지에 넣을 수 있는지 확인
lv_v1 = r_count2 - lv_cnt_1
If lv_v1 > 0 Then
'--- 2. 2페이지에 넣을 수 있는지 확인
lv_v2 = r_count2 - lv_cnt_2 - lv_cnt_4
If lv_v2 > 0 Then
lv_d1 = lv_v2 \ lv_cnt_3
lv_f1 = lv_v2 Mod lv_cnt_3
If lv_f1 > 0 Then
totalPageCnt = lv_d1 + 3
Else
totalPageCnt = lv_d1 + 2
End If
Else
totalPageCnt = 2
End If
Else
totalPageCnt = 1
End If
' 2. 페이지 copy and paste
'1. copy and set
startRow = 1
endRow = onePageCnt
activRow = 1
endRow8 = endRow - 7
ThisWorkbook.Sheets(1).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
If totalPageCnt > 1 Then
' 첫 페이지를 copy
Range("A" & startRow & ":" & "M" & endRow).Select
Selection.Copy
For lv_1 = 1 To totalPageCnt - 1
activRow = activRow + onePageCnt
Cells(activRow, 1).Select
ActiveSheet.Paste
startRow = startRow + onePageCnt
endRow = endRow + onePageCnt
endRow9 = endRow - bottomPageCnt - 1
Rows(endRow9 & ":" & endRow).Select
Selection.RowHeight = 12.5
endRow8 = endRow9 + 3
ThisWorkbook.Sheets(1).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
Next lv_1
'2. 2번째 페이지의 firstTopPage를 삭제
If firstTopPageCnt > 0 Then
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 1
itemendRow = itemstartRow + firstTopPageCnt - 1
itemstartRow_V2 = itemstartRow + 1
itemendRow_V2 = itemstartRow_V2 + 1
For lv_1 = 1 To totalPageCnt
If lv_1 > 1 Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = firstTopstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
Range("A" & itemstartRow_V2 & ":" & "M" & itemendRow_V2).Select
Selection.Copy
activRow = itemstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
firstTopstartRow = firstTopstartRow + onePageCnt
firstTopendRow = firstTopendRow + onePageCnt
'2번째 페이지부터 GST Registration No 삭제
clearrow = firstTopstartRow - 1
If firstTopstartRow <> topPageCnt Then
Range("B" & clearrow & ":" & "D" & clearrow).Select
Selection.ClearContents
End If
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
itemstartRow_V2 = itemstartRow_V2 + onePageCnt
itemendRow_V2 = itemendRow_V2 + onePageCnt
Next lv_1
End If
'3. 2번째 페이지가 마지막 페이지 아닐 경우 lastBottomPage를 삭제
If lastBottomPageCnt > 0 Then
lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
itemstartRow = lastBottomstartRow - lastBottomPageCnt - 1
itemendRow = itemstartRow + lastBottomPageCnt
For lv_1 = 1 To totalPageCnt
If lv_1 <> totalPageCnt Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = itemendRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
lastBottomstartRow = lastBottomstartRow + onePageCnt
lastBottomendRow = lastBottomendRow + onePageCnt
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
Next lv_1
' 최종 sub 바닥글의 위치 정보
'last_LastStaLine = lastBottomstartRow - onePageCnt
'last_LastEndLine = lastBottomendRow - onePageCnt - 1
End If
'Else
'lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
'lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
'last_LastStaLine = lastBottomstartRow
'last_LastEndLine = lastBottomendRow - 1
End If
' 3. item data 출력 하기
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 2
currPage = 1
For itemLine = 1 To r_count2
pageLine = pageLine + 1
If currPage = 1 Then
'첫 페이지
If totalPageCnt = currPage Then
If pageLine > lv_cnt_1 Then '첫 페이지가 마지막 페이지가 일 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
Else
If pageLine > lv_cnt_2 Then '첫 페이지가 마지막 페이지가 아닐 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
ElseIf totalPageCnt = currPage Then
' 마지막 페이지
Else
' 기타 페이지
If pageLine > lv_cnt_3 Then
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
'==========> 여기서 item을 출력 하면 됩니다. ==========<
Range("D" & itemstartRow & ":" & "F" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("I" & itemstartRow & ":" & "K" & itemstartRow).Select
Selection.UnMerge
Range("H" & itemstartRow & ":" & "I" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("L" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("A" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlLeft
Range("G" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlRight
ThisWorkbook.Sheets(1).Cells(itemstartRow, 2) = r3table_2.Value(itemLine, 1) 'Line
ThisWorkbook.Sheets(1).Cells(itemstartRow, 4) = r3table_2.Value(itemLine, 2) 'Description
ThisWorkbook.Sheets(1).Cells(itemstartRow, 7) = r3table_2.Value(itemLine, 3) 'Quantity
ThisWorkbook.Sheets(1).Cells(itemstartRow, 8) = r3table_2.Value(itemLine, 4) 'Unit Price
ThisWorkbook.Sheets(1).Cells(itemstartRow, 11) = r3table_2.Value(itemLine, 7) 'Tax
ThisWorkbook.Sheets(1).Cells(itemstartRow, 12) = r3table_2.Value(itemLine, 5) 'Amount
Range("K" & itemstartRow).Select
Selection.NumberFormatLocal = "0%"
Range("G" & itemstartRow & ":" & "I" & itemstartRow & "," & "L" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.NumberFormatLocal = "#,##0.00"
itemstartRow = itemstartRow + 1
Next itemLine
' 3. last 바닥글의 위치를 조정 하기
' item의 마지막 라인
'itemEndLine = itemstartRow + 3
'Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
'Selection.Copy
'Cells(itemEndLine, 1).Select
'ActiveSheet.Paste
''전에 있던 last 바닥글을 지우기
'Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
'Selection.Clear
' 4. 프린트 페이지를 지정하기
plt = totalPageCnt * onePageCnt '최종 페이지 라인
' 4-1 최종 페이지 지정
ActiveSheet.PageSetup.PrintArea = "A1:" & dispCell & plt '최종 페이지의 프린트 영역지정
' 4-2 페이지 분할 개수
pageBreakCnt = ActiveWindow.SelectedSheets.HPageBreaks.Count
' 4-3 페이지 분할 위치 지정 하기
For l2 = 1 To totalPageCnt
' 분할 페이지 이치
page_line = onePageCnt * l2
plt = page_line + 1 ' -1 프린트 페이지 수
' 분할 페이지 라인이 부족 시 분할 라인 추가
If l2 > pageBreakCnt Then
Cells(plt, 1).Select
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell ' 프린트 페이지 삽입
End If
' 최종 분할 라인 위치를 를 지정
If l2 <> totalPageCnt Then
Set ActiveSheet.HPageBreaks(l2).Location = Range("A" & plt)
End If
Next l2
' 5. 페이지 / 총 페이지 출력
dispPageRow = 10 '2 'Page Row(시작)
dispPageCell = 9 '14 'Page Column
For L1 = 1 To totalPageCnt
Cells(dispPageRow, dispPageCell).Select
pagenumber '페이지 넘버 출력
dispPageRow = dispPageRow + onePageCnt
Next L1
End Sub
Sub R3_ITME_Sheet4()
Dim r_count1 As Long 'Head
Dim r_count2 As Long 'Item
' Dim r_count3 As Long 'job Order
Dim r3table_1 As Object '
Dim r3table_2 As Object '
' Dim r3table_3 As Object '
Dim onePageCnt As Long '한 page의 라인 수
Dim topPageCnt As Long 'top page의 라인수
Dim firstTopPageCnt As Long 'sub top page는 첫 page일 때만 출력
Dim lastBottomPageCnt As Long '마지막 page 시 사용하는 바닥글
Dim bottomPageCnt As Long '바닥글 라인 수
Dim totalPageCnt As Long '토탈 페이지수
'Dim last_itemEndLine As Long 'item의 마지막 라인
'Dim last_LastStaLine As Long '마지막 page 시 사용하는 바닥글 시작 라인
'Dim last_LastEndLine As Long '마지막 page 시 사용하는 바닥글 마지막 라인
Set r3table_1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table
Set r3table_2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table
' Set r3table_3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table
r_count1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table.RowCount
r_count2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table.RowCount
' r_count3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table.RowCount
' 0. (중복으로 출력할) 해더&바텀 DATA 미리 입력 하기
ThisWorkbook.Sheets(2).Cells(6, 2) = r3table_1.Value(1, 1) 'Order From
ThisWorkbook.Sheets(2).Cells(6, 6) = r3table_1.Value(1, 2) 'Deliver To
ThisWorkbook.Sheets(2).Cells(7, 11) = r3table_1.Value(1, 3) 'PO
ThisWorkbook.Sheets(2).Cells(8, 11) = r3table_1.Value(1, 4) 'Date
'ThisWorkbook.Sheets(2).Cells(7, 10) = r3table_1.Value(1, 5) 'JO
ThisWorkbook.Sheets(2).Cells(10, 2) = r3table_1.Value(1, 26) 'GST Registration No.
ThisWorkbook.Sheets(2).Cells(10, 4) = r3table_1.Value(1, 6) 'GST Registration No.
ThisWorkbook.Sheets(2).Cells(11, 3) = r3table_1.Value(1, 24) 'Attn
ThisWorkbook.Sheets(2).Cells(12, 3) = r3table_1.Value(1, 7) 'Phone
ThisWorkbook.Sheets(2).Cells(13, 3) = r3table_1.Value(1, 8) 'Email
ThisWorkbook.Sheets(2).Cells(11, 11) = r3table_1.Value(1, 9) 'Service Client
ThisWorkbook.Sheets(2).Cells(12, 11) = r3table_1.Value(1, 10) 'Client Reference1
ThisWorkbook.Sheets(2).Cells(13, 11) = r3table_1.Value(1, 11) 'Client Reference2
ThisWorkbook.Sheets(2).Cells(14, 11) = r3table_1.Value(1, 12) 'Client Reference3
ThisWorkbook.Sheets(2).Cells(17, 2) = r3table_1.Value(1, 13) 'Delivery Date
ThisWorkbook.Sheets(2).Cells(17, 5) = r3table_1.Value(1, 14) 'Payment Term
ThisWorkbook.Sheets(2).Cells(17, 7) = r3table_1.Value(1, 15) 'Currency
ThisWorkbook.Sheets(2).Cells(17, 9) = r3table_1.Value(1, 16) 'Exchange Rate
'ThisWorkbook.Sheets(2).Cells(49, 4) = r3table_1.Value(1, 17) 'Workflow Status
'ThisWorkbook.Sheets(2).Cells(51, 4) = r3table_1.Value(1, 18) 'Requested by
'ThisWorkbook.Sheets(2).Cells(52, 4) = r3table_1.Value(1, 19) 'Approved by
' Total
ThisWorkbook.Sheets(2).Cells(48, 13) = r3table_1.Value(1, 20) 'Lines subtotal
ThisWorkbook.Sheets(2).Cells(49, 13) = r3table_1.Value(1, 21) 'Misc. Charge Subtotal
ThisWorkbook.Sheets(2).Cells(50, 13) = r3table_1.Value(1, 25) 'Add GST
ThisWorkbook.Sheets(2).Cells(51, 12) = r3table_1.Value(1, 15) 'Currency
ThisWorkbook.Sheets(2).Cells(51, 13) = r3table_1.Value(1, 22) 'Total
' SK Adress 바닥들
' ThisWorkbook.Sheets(2).Cells(48, 1) = r3table_1.Value(1, 23) 'Detail
' ThisWorkbook.Sheets(2).Cells(49, 1) = r3table_1.Value(1, 24) 'Detail
' ThisWorkbook.Sheets(2).Cells(50, 1) = r3table_1.Value(1, 25) 'Detail
' 0. Area 라인
onePageCnt = 62 ' 한 page의 라인 수
topPageCnt = 10 'top page의 라인수
firstTopPageCnt = 8 ' sub top page는 첫 page일 때만 출력
lastBottomPageCnt = 6 '마지막 page 시 사용하는 바닥글
bottomPageCnt = 9 '바닥글 라인 수
dispCell = "M" '출력마지막 CELL
If r3table_1.Value(1, 26) <> "" Then 'GST Registration No.
Range("D10").Select
Selection.HorizontalAlignment = xlLeft
Selection.VerticalAlignment = xlCenter
End If
' 1. 총 페이지수를 계산 하기
'r_count2 = 52 'item의 라인수
'1. 첫페이지만 있을 경우의 item 가능 라인수
lv_cnt_1 = onePageCnt - topPageCnt - firstTopPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'2. 첫페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_2 = onePageCnt - topPageCnt - firstTopPageCnt - bottomPageCnt - 1
'3. 2번째 페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_3 = onePageCnt - topPageCnt - bottomPageCnt - 1
'4. 2번째 페이지가 마지막 페이지 일 경우 item 가능 라인수
lv_cnt_4 = onePageCnt - topPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'--- 1. 1페이지에 넣을 수 있는지 확인
lv_v1 = r_count2 - lv_cnt_1
If lv_v1 > 0 Then
'--- 2. 2페이지에 넣을 수 있는지 확인
lv_v2 = r_count2 - lv_cnt_2 - lv_cnt_4
If lv_v2 > 0 Then
lv_d1 = lv_v2 \ lv_cnt_3
lv_f1 = lv_v2 Mod lv_cnt_3
If lv_f1 > 0 Then
totalPageCnt = lv_d1 + 3
Else
totalPageCnt = lv_d1 + 2
End If
Else
totalPageCnt = 2
End If
Else
totalPageCnt = 1
End If
' 2. 페이지 copy and paste
'1. copy and set
startRow = 1
endRow = onePageCnt
activRow = 1
endRow8 = endRow - 7
ThisWorkbook.Sheets(2).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
If totalPageCnt > 1 Then
' 첫 페이지를 copy
Range("A" & startRow & ":" & "M" & endRow).Select
Selection.Copy
For lv_1 = 1 To totalPageCnt - 1
activRow = activRow + onePageCnt
Cells(activRow, 1).Select
ActiveSheet.Paste
startRow = startRow + onePageCnt
endRow = endRow + onePageCnt
endRow9 = endRow - bottomPageCnt - 1
Rows(endRow9 & ":" & endRow).Select
Selection.RowHeight = 12.5
endRow8 = endRow9 + 3
ThisWorkbook.Sheets(2).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
Next lv_1
'2. 2번째 페이지의 firstTopPage를 삭제
If firstTopPageCnt > 0 Then
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 1
itemendRow = itemstartRow + firstTopPageCnt - 1
itemstartRow_V2 = itemstartRow + 1
itemendRow_V2 = itemstartRow_V2 + 1
For lv_1 = 1 To totalPageCnt
If lv_1 > 1 Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = firstTopstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
Range("A" & itemstartRow_V2 & ":" & "M" & itemendRow_V2).Select
Selection.Copy
activRow = itemstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
firstTopstartRow = firstTopstartRow + onePageCnt
firstTopendRow = firstTopendRow + onePageCnt
'2번째 페이지부터 GST Registration No 삭제
clearrow = firstTopstartRow - 1
If firstTopstartRow <> topPageCnt Then
Range("B" & clearrow & ":" & "D" & clearrow).Select
Selection.ClearContents
End If
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
itemstartRow_V2 = itemstartRow_V2 + onePageCnt
itemendRow_V2 = itemendRow_V2 + onePageCnt
Next lv_1
End If
'3. 2번째 페이지가 마지막 페이지 아닐 경우 lastBottomPage를 삭제
If lastBottomPageCnt > 0 Then
lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
itemstartRow = lastBottomstartRow - lastBottomPageCnt - 1
itemendRow = itemstartRow + lastBottomPageCnt
For lv_1 = 1 To totalPageCnt
If lv_1 <> totalPageCnt Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = itemendRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
lastBottomstartRow = lastBottomstartRow + onePageCnt
lastBottomendRow = lastBottomendRow + onePageCnt
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
Next lv_1
' 최종 sub 바닥글의 위치 정보
'last_LastStaLine = lastBottomstartRow - onePageCnt
'last_LastEndLine = lastBottomendRow - onePageCnt - 1
End If
'Else
'lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
'lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
'last_LastStaLine = lastBottomstartRow
'last_LastEndLine = lastBottomendRow - 1
End If
' 3. item data 출력 하기
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 2
currPage = 1
For itemLine = 1 To r_count2
pageLine = pageLine + 1
If currPage = 1 Then
'첫 페이지
If totalPageCnt = currPage Then
If pageLine > lv_cnt_1 Then '첫 페이지가 마지막 페이지가 일 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
Else
If pageLine > lv_cnt_2 Then '첫 페이지가 마지막 페이지가 아닐 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
ElseIf totalPageCnt = currPage Then
' 마지막 페이지
Else
' 기타 페이지
If pageLine > lv_cnt_3 Then
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
'==========> 여기서 item을 출력 하면 됩니다. ==========<
Range("D" & itemstartRow & ":" & "F" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("I" & itemstartRow & ":" & "K" & itemstartRow).Select
Selection.UnMerge
Range("H" & itemstartRow & ":" & "I" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("L" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("A" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlLeft
Range("G" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlRight
ThisWorkbook.Sheets(2).Cells(itemstartRow, 2) = r3table_2.Value(itemLine, 6) 'Line
ThisWorkbook.Sheets(2).Cells(itemstartRow, 4) = r3table_2.Value(itemLine, 2) 'Description
ThisWorkbook.Sheets(2).Cells(itemstartRow, 7) = r3table_2.Value(itemLine, 3) 'Quantity
ThisWorkbook.Sheets(2).Cells(itemstartRow, 8) = r3table_2.Value(itemLine, 4) 'Unit Price
ThisWorkbook.Sheets(2).Cells(itemstartRow, 11) = r3table_2.Value(itemLine, 7) 'Tax
ThisWorkbook.Sheets(2).Cells(itemstartRow, 12) = r3table_2.Value(itemLine, 5) 'Amount
Range("K" & itemstartRow).Select
Selection.NumberFormatLocal = "0%"
Range("G" & itemstartRow & ":" & "I" & itemstartRow & "," & "L" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.NumberFormatLocal = "#,##0.00"
itemstartRow = itemstartRow + 1
Next itemLine
' 3. last 바닥글의 위치를 조정 하기
' item의 마지막 라인
'itemEndLine = itemstartRow + 3
'Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
'Selection.Copy
'Cells(itemEndLine, 1).Select
'ActiveSheet.Paste
''전에 있던 last 바닥글을 지우기
'Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
'Selection.Clear
' 4. 프린트 페이지를 지정하기
plt = totalPageCnt * onePageCnt '최종 페이지 라인
' 4-1 최종 페이지 지정
'ActiveSheet.PageSetup.PrintArea = "A1:" & dispCell & plt '최종 페이지의 프린트 영역지정
ThisWorkbook.Sheets(2).PageSetup.PrintArea = "A1:" & dispCell & plt '최종 페이지의 프린트 영역지정
' 4-2 페이지 분할 개수
pageBreakCnt = ActiveWindow.SelectedSheets.HPageBreaks.Count
' 4-3 페이지 분할 위치 지정 하기
For l2 = 1 To totalPageCnt
' 분할 페이지 이치
page_line = onePageCnt * l2
plt = page_line + 1 ' -1 프린트 페이지 수
' 분할 페이지 라인이 부족 시 분할 라인 추가
If l2 > pageBreakCnt Then
Cells(plt, 1).Select
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell ' 프린트 페이지 삽입
End If
' 최종 분할 라인 위치를 를 지정
If l2 <> totalPageCnt Then
Set ActiveSheet.HPageBreaks(l2).Location = Range("A" & plt)
End If
Next l2
' 5. 페이지 / 총 페이지 출력
dispPageRow = 10 '2 'Page Row(시작)
dispPageCell = 9 '14 'Page Column
For L1 = 1 To totalPageCnt
Cells(dispPageRow, dispPageCell).Select
pagenumber '페이지 넘버 출력
dispPageRow = dispPageRow + onePageCnt
Next L1
End Sub
Sub R3_ITME_Sheet2()
Dim r_count1 As Long 'Head
' Dim r_count2 As Long 'Item
Dim r_count3 As Long 'job Order
Dim r3table_1 As Object '
' Dim r3table_2 As Object '
Dim r3table_3 As Object '
Dim onePageCnt As Long '한 page의 라인 수
Dim topPageCnt As Long 'top page의 라인수
Dim firstTopPageCnt As Long 'sub top page는 첫 page일 때만 출력
Dim lastBottomPageCnt As Long '마지막 page 시 사용하는 바닥글
Dim bottomPageCnt As Long '바닥글 라인 수
Dim totalPageCnt As Long '토탈 페이지수
Dim last_itemEndLine As Long 'item의 마지막 라인
Dim last_LastStaLine As Long '마지막 page 시 사용하는 바닥글 시작 라인
Dim last_LastEndLine As Long '마지막 page 시 사용하는 바닥글 마지막 라인
Set r3table_1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table
' Set r3table_2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table
Set r3table_3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table
r_count1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table.RowCount
' r_count2 = ThisWorkbook.Container.LinkServer.Items("ITEM").Table.RowCount
r_count3 = ThisWorkbook.Container.LinkServer.Items("ZJOB").Table.RowCount
' 0. (중복으로 출력할) 해더&바텀 DATA 미리 입력 하기
ThisWorkbook.Sheets(3).Cells(6, 2) = r3table_1.Value(1, 1) 'Order From
ThisWorkbook.Sheets(3).Cells(6, 6) = r3table_1.Value(1, 2) 'Deliver To
ThisWorkbook.Sheets(3).Cells(7, 11) = r3table_1.Value(1, 3) 'PO
ThisWorkbook.Sheets(3).Cells(8, 11) = r3table_1.Value(1, 4) 'Date
'ThisWorkbook.Sheets(3).Cells(7, 10) = r3table_1.Value(1, 5) 'JO
'ThisWorkbook.Sheets(3).Cells(9, 12) = r3table_1.Value(1, 6) 'Page
' ThisWorkbook.Sheets(3).Cells(12, 3) = r3table_1.Value(1, 7) 'Phone
' ThisWorkbook.Sheets(3).Cells(13, 3) = r3table_1.Value(1, 8) 'Email
' ThisWorkbook.Sheets(3).Cells(11, 11) = r3table_1.Value(1, 9) 'Service Client
' ThisWorkbook.Sheets(3).Cells(12, 11) = r3table_1.Value(1, 10) 'Client Reference1
' ThisWorkbook.Sheets(3).Cells(13, 11) = r3table_1.Value(1, 11) 'Client Reference2
' ThisWorkbook.Sheets(3).Cells(14, 11) = r3table_1.Value(1, 12) 'Client Reference3
' ThisWorkbook.Sheets(3).Cells(17, 2) = r3table_1.Value(1, 13) 'Delivery Date
' ThisWorkbook.Sheets(3).Cells(17, 5) = r3table_1.Value(1, 14) 'Payment Term
' ThisWorkbook.Sheets(3).Cells(17, 7) = r3table_1.Value(1, 15) 'Currency
' ThisWorkbook.Sheets(3).Cells(17, 10) = r3table_1.Value(1, 16) 'Exchange Rate
' ThisWorkbook.Sheets(3).Cells(39, 4) = r3table_1.Value(1, 17) 'Workflow Status
' ThisWorkbook.Sheets(3).Cells(41, 4) = r3table_1.Value(1, 18) 'Requested by
' ThisWorkbook.Sheets(3).Cells(42, 4) = r3table_1.Value(1, 19) 'Approved by
' Total
' ThisWorkbook.Sheets(3).Cells(38, 13) = r3table_1.Value(1, 20) 'Lines subtotal
' ThisWorkbook.Sheets(3).Cells(39, 13) = r3table_1.Value(1, 21) 'Tax(GST/VAT etc)
' ThisWorkbook.Sheets(3).Cells(41, 12) = r3table_1.Value(1, 15) 'Currency
' ThisWorkbook.Sheets(3).Cells(41, 13) = r3table_1.Value(1, 22) 'Total
' SK Adress 바닥들
' ThisWorkbook.Sheets(3).Cells(48, 1) = r3table_1.Value(1, 23) 'Detail
' ThisWorkbook.Sheets(3).Cells(49, 1) = r3table_1.Value(1, 24) 'Detail
' ThisWorkbook.Sheets(3).Cells(50, 1) = r3table_1.Value(1, 25) 'Detail
' 0. Area 라인
onePageCnt = 62 ' 한 page의 라인 수
topPageCnt = 11 'top page의 라인수
firstTopPageCnt = 0 ' sub top page는 첫 page일 때만 출력
lastBottomPageCnt = 0 '마지막 page 시 사용하는 바닥글
bottomPageCnt = 9 '바닥글 라인 수
dispCell = "M" '출력마지막 CELL
' 1. 총 페이지수를 계산 하기
'r_count2 = 52 'item의 라인수
'1. 첫페이지만 있을 경우의 item 가능 라인수
lv_cnt_1 = onePageCnt - topPageCnt - firstTopPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'2. 첫페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_2 = onePageCnt - topPageCnt - firstTopPageCnt - bottomPageCnt - 1
'3. 2번째 페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_3 = onePageCnt - topPageCnt - bottomPageCnt - 1
'4. 2번째 페이지가 마지막 페이지 일 경우 item 가능 라인수
lv_cnt_4 = onePageCnt - topPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'--- 1. 1페이지에 넣을 수 있는지 확인
lv_v1 = r_count3 - lv_cnt_1
If lv_v1 > 0 Then
'--- 2. 2페이지에 넣을 수 있는지 확인
lv_v2 = r_count3 - lv_cnt_2 - lv_cnt_4
If lv_v2 > 0 Then
lv_d1 = lv_v2 \ lv_cnt_3
lv_f1 = lv_v2 Mod lv_cnt_3
If lv_f1 > 0 Then
totalPageCnt = lv_d1 + 3
Else
totalPageCnt = lv_d1 + 2
End If
Else
totalPageCnt = 2
End If
Else
totalPageCnt = 1
End If
' 2. 페이지 copy and paste
'1. copy and set
startRow = 1
endRow = onePageCnt
activRow = 1
endRow8 = endRow - 7
ThisWorkbook.Sheets(3).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
If totalPageCnt > 1 Then
' 첫 페이지를 copy
Range("A" & startRow & ":" & "M" & endRow).Select
Selection.Copy
For lv_1 = 1 To totalPageCnt - 1
activRow = activRow + onePageCnt
Cells(activRow, 1).Select
ActiveSheet.Paste
startRow = startRow + onePageCnt
endRow = endRow + onePageCnt
endRow9 = endRow - bottomPageCnt - 1
Rows(endRow9 & ":" & endRow).Select
Selection.RowHeight = 12.5
endRow8 = endRow9 + 3
ThisWorkbook.Sheets(3).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
Next lv_1
'2. 2번째 페이지의 firstTopPage를 삭제
If firstTopPageCnt > 0 Then
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 1
itemendRow = itemstartRow + firstTopPageCnt - 1
itemstartRow_V2 = itemstartRow + 1
itemendRow_V2 = itemstartRow_V2 + 1
For lv_1 = 1 To totalPageCnt
If lv_1 > 1 Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = firstTopstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
Range("A" & itemstartRow_V2 & ":" & "M" & itemendRow_V2).Select
Selection.Copy
activRow = itemstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
firstTopstartRow = firstTopstartRow + onePageCnt
firstTopendRow = firstTopendRow + onePageCnt
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
itemstartRow_V2 = itemstartRow_V2 + onePageCnt
itemendRow_V2 = itemendRow_V2 + onePageCnt
Next lv_1
End If
'3. 2번째 페이지가 마지막 페이지 아닐 경우 lastBottomPage를 삭제
If lastBottomPageCnt > 0 Then
lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
itemstartRow = lastBottomstartRow - lastBottomPageCnt - 1
itemendRow = itemstartRow + lastBottomPageCnt
For lv_1 = 1 To totalPageCnt
If lv_1 <> totalPageCnt Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = itemendRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
lastBottomstartRow = lastBottomstartRow + onePageCnt
lastBottomendRow = lastBottomendRow + onePageCnt
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
Next lv_1
' 최종 sub 바닥글의 위치 정보
last_LastStaLine = lastBottomstartRow - onePageCnt
last_LastEndLine = lastBottomendRow - onePageCnt - 1
End If
Else
lastBottomstartRow = onePageCnt - bottomPageCnt - lastBottomPageCnt + 1
lastBottomendRow = lastBottomstartRow + firstTopPageCnt - 1
last_LastStaLine = lastBottomstartRow
last_LastEndLine = lastBottomendRow - 1
End If
' 3. item data 출력 하기
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 2
currPage = 1
For itemLine = 1 To r_count3
pageLine = pageLine + 1
If currPage = 1 Then
'첫 페이지
If totalPageCnt = currPage Then
If pageLine > lv_cnt_1 Then '첫 페이지가 마지막 페이지가 일 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
Else
If pageLine > lv_cnt_2 Then '첫 페이지가 마지막 페이지가 아닐 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
ElseIf totalPageCnt = currPage Then
' 마지막 페이지
Else
' 기타 페이지
If pageLine > lv_cnt_3 Then
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
'==========> 여기서 item을 출력 하면 됩니다. ==========<
Range("H" & itemstartRow & ":" & "J" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("K" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("A" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlLeft
ThisWorkbook.Sheets(3).Cells(itemstartRow, 2) = r3table_3.Value(itemLine, 1) '
ThisWorkbook.Sheets(3).Cells(itemstartRow, 3) = r3table_3.Value(itemLine, 2) '
ThisWorkbook.Sheets(3).Cells(itemstartRow, 5) = r3table_3.Value(itemLine, 3) '
ThisWorkbook.Sheets(3).Cells(itemstartRow, 7) = r3table_3.Value(itemLine, 4) '
ThisWorkbook.Sheets(3).Cells(itemstartRow, 8) = r3table_3.Value(itemLine, 5) '
ThisWorkbook.Sheets(3).Cells(itemstartRow, 11) = r3table_3.Value(itemLine, 6) '
itemstartRow = itemstartRow + 1
Next itemLine
' 3. last 바닥글의 위치를 조정 하기
' item의 마지막 라인
' itemEndLine = itemstartRow + 3
' Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
' Selection.Copy
' Cells(itemEndLine, 1).Select
' ActiveSheet.Paste
'전에 있던 last 바닥글을 지우기
' Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
' Selection.Clear
' 4. 프린트 페이지를 지정하기
Dim plt As Long
Dim pageBreakCntV2 As Long
Dim hpPlt As Long
plt = totalPageCnt * onePageCnt '최종 페이지 라인
' 4-1 최종 페이지 지정
ThisWorkbook.Sheets(3).PageSetup.PrintArea = "A1:" & dispCell & plt '최종 페이지의 프린트 영역지정
' 4-2 페이지 분할 개수
pageBreakCntV2 = ThisWorkbook.Sheets(3).HPageBreaks.Count
' 4-3 페이지 분할 위치 지정 하기
For hpact = 1 To pageBreakCntV2
' 분할 페이지 이치
page_line = onePageCnt * hpact
hpPlt = page_line + 1 ' -1 프린트 페이지 수
' 분할 페이지 라인이 부족 시 분할 라인 추가
If hpact > pageBreakCntV2 Then
ThisWorkbook.Sheets(3).Cells(hpPlt, 1).Select
ThisWorkbook.Sheets(3).HPageBreaks.Add Before:=ActiveCell ' 프린트 페이지 삽입
End If
' 최종 분할 라인 위치를 를 지정
If hpact <> totalPageCnt Then
Application.ScreenUpdating = False
ActiveWindow.View = xlPageBreakPreview
Set ActiveSheet.HPageBreaks(hpact).Location = Range("A" & hpPlt)
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
'Set ThisWorkbook.Sheets(3).HPageBreaks(hpact).Location = ThisWorkbook.Sheets(3).Range("A" & hpPlt)
End If
Next hpact
' 5. 페이지 / 총 페이지 출력
dispPageRow = 10 '2 'Page Row(시작)
dispPageCell = 9 '14 'Page Column
For L1 = 1 To totalPageCnt
Cells(dispPageRow, dispPageCell).Select
pagenumber '페이지 넘버 출력
dispPageRow = dispPageRow + onePageCnt
Next L1
End Sub
Sub R3_ITME_Sheet3()
Dim r_count1 As Long 'Head
Dim r_count4 As Long 'job Order Sum
Dim r3table_1 As Object '
Dim r3table_4 As Object '
Dim onePageCnt As Long '한 page의 라인 수
Dim topPageCnt As Long 'top page의 라인수
Dim firstTopPageCnt As Long 'sub top page는 첫 page일 때만 출력
Dim lastBottomPageCnt As Long '마지막 page 시 사용하는 바닥글
Dim bottomPageCnt As Long '바닥글 라인 수
Dim totalPageCnt As Long '토탈 페이지수
Dim last_itemEndLine As Long 'item의 마지막 라인
Dim last_LastStaLine As Long '마지막 page 시 사용하는 바닥글 시작 라인
Dim last_LastEndLine As Long '마지막 page 시 사용하는 바닥글 마지막 라인
Set r3table_1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table
Set r3table_4 = ThisWorkbook.Container.LinkServer.Items("ZSUM").Table
r_count1 = ThisWorkbook.Container.LinkServer.Items("HEAD").Table.RowCount
r_count4 = ThisWorkbook.Container.LinkServer.Items("ZSUM").Table.RowCount
' 0. (중복으로 출력할) 해더&바텀 DATA 미리 입력 하기
ThisWorkbook.Sheets(4).Cells(6, 2) = r3table_1.Value(1, 1) 'Order From
ThisWorkbook.Sheets(4).Cells(6, 6) = r3table_1.Value(1, 2) 'Deliver To
ThisWorkbook.Sheets(4).Cells(7, 11) = r3table_1.Value(1, 3) 'PO
ThisWorkbook.Sheets(4).Cells(8, 11) = r3table_1.Value(1, 4) 'Date
'ThisWorkbook.Sheets(4).Cells(7, 10) = r3table_1.Value(1, 5) 'JO
'ThisWorkbook.Sheets(4).Cells(9, 12) = r3table_1.Value(1, 6) 'Page
' ThisWorkbook.Sheets(4).Cells(12, 3) = r3table_1.Value(1, 7) 'Phone
' ThisWorkbook.Sheets(4).Cells(13, 3) = r3table_1.Value(1, 8) 'Email
' ThisWorkbook.Sheets(4).Cells(11, 11) = r3table_1.Value(1, 9) 'Service Client
' ThisWorkbook.Sheets(4).Cells(12, 11) = r3table_1.Value(1, 10) 'Client Reference1
' ThisWorkbook.Sheets(4).Cells(13, 11) = r3table_1.Value(1, 11) 'Client Reference2
' ThisWorkbook.Sheets(4).Cells(14, 11) = r3table_1.Value(1, 12) 'Client Reference3
' ThisWorkbook.Sheets(4).Cells(17, 2) = r3table_1.Value(1, 13) 'Delivery Date
' ThisWorkbook.Sheets(4).Cells(17, 5) = r3table_1.Value(1, 14) 'Payment Term
' ThisWorkbook.Sheets(4).Cells(17, 7) = r3table_1.Value(1, 15) 'Currency
' ThisWorkbook.Sheets(4).Cells(17, 10) = r3table_1.Value(1, 16) 'Exchange Rate
' ThisWorkbook.Sheets(4).Cells(39, 4) = r3table_1.Value(1, 17) 'Workflow Status
' ThisWorkbook.Sheets(4).Cells(41, 4) = r3table_1.Value(1, 18) 'Requested by
' ThisWorkbook.Sheets(4).Cells(42, 4) = r3table_1.Value(1, 19) 'Approved by
' Total
' ThisWorkbook.Sheets(4).Cells(38, 13) = r3table_1.Value(1, 20) 'Lines subtotal
' ThisWorkbook.Sheets(4).Cells(39, 13) = r3table_1.Value(1, 21) 'Tax(GST/VAT etc)
' ThisWorkbook.Sheets(4).Cells(41, 12) = r3table_1.Value(1, 15) 'Currency
' ThisWorkbook.Sheets(4).Cells(41, 13) = r3table_1.Value(1, 22) 'Total
' SK Adress 바닥들
' ThisWorkbook.Sheets(4).Cells(48, 1) = r3table_1.Value(1, 23) 'Detail
' ThisWorkbook.Sheets(4).Cells(49, 1) = r3table_1.Value(1, 24) 'Detail
' ThisWorkbook.Sheets(4).Cells(50, 1) = r3table_1.Value(1, 25) 'Detail
' 0. Area 라인
onePageCnt = 62 ' 한 page의 라인 수
topPageCnt = 11 'top page의 라인수
firstTopPageCnt = 0 ' sub top page는 첫 page일 때만 출력
lastBottomPageCnt = 0 '마지막 page 시 사용하는 바닥글
bottomPageCnt = 9 '바닥글 라인 수
dispCell = "M" '출력마지막 CELL
' 1. 총 페이지수를 계산 하기
'r_count2 = 52 'item의 라인수
'1. 첫페이지만 있을 경우의 item 가능 라인수
lv_cnt_1 = onePageCnt - topPageCnt - firstTopPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'2. 첫페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_2 = onePageCnt - topPageCnt - firstTopPageCnt - bottomPageCnt - 1
'3. 2번째 페이지가 마지막 페이지 아닐 경우 item 가능 라인수
lv_cnt_3 = onePageCnt - topPageCnt - bottomPageCnt - 1
'4. 2번째 페이지가 마지막 페이지 일 경우 item 가능 라인수
lv_cnt_4 = onePageCnt - topPageCnt - lastBottomPageCnt - bottomPageCnt - 1
'--- 1. 1페이지에 넣을 수 있는지 확인
lv_v1 = r_count4 - lv_cnt_1
If lv_v1 > 0 Then
'--- 2. 2페이지에 넣을 수 있는지 확인
lv_v2 = r_count4 - lv_cnt_2 - lv_cnt_4
If lv_v2 > 0 Then
lv_d1 = lv_v2 \ lv_cnt_3
lv_f1 = lv_v2 Mod lv_cnt_3
If lv_f1 > 0 Then
totalPageCnt = lv_d1 + 3
Else
totalPageCnt = lv_d1 + 2
End If
Else
totalPageCnt = 2
End If
Else
totalPageCnt = 1
End If
' 2. 페이지 copy and paste
'1. copy and set
startRow = 1
endRow = onePageCnt
activRow = 1
endRow8 = endRow - 7
ThisWorkbook.Sheets(4).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
If totalPageCnt > 1 Then
' 첫 페이지를 copy
Range("A" & startRow & ":" & "M" & endRow).Select
Selection.Copy
For lv_1 = 1 To totalPageCnt - 1
activRow = activRow + onePageCnt
Cells(activRow, 1).Select
ActiveSheet.Paste
startRow = startRow + onePageCnt
endRow = endRow + onePageCnt
endRow9 = endRow - bottomPageCnt - 1
Rows(endRow9 & ":" & endRow).Select
Selection.RowHeight = 12.5
endRow8 = endRow9 + 3
ThisWorkbook.Sheets(4).Cells(endRow8, 2) = r3table_1.Value(1, 23) 'BUTXT
Next lv_1
'2. 2번째 페이지의 firstTopPage를 삭제
If firstTopPageCnt > 0 Then
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 1
itemendRow = itemstartRow + firstTopPageCnt - 1
itemstartRow_V2 = itemstartRow + 1
itemendRow_V2 = itemstartRow_V2 + 1
For lv_1 = 1 To totalPageCnt
If lv_1 > 1 Then
Range("A" & itemstartRow & ":" & "M" & itemendRow).Select
Selection.Copy
activRow = firstTopstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
Range("A" & itemstartRow_V2 & ":" & "M" & itemendRow_V2).Select
Selection.Copy
activRow = itemstartRow
Cells(activRow, 1).Select
ActiveSheet.Paste
End If
firstTopstartRow = firstTopstartRow + onePageCnt
firstTopendRow = firstTopendRow + onePageCnt
itemstartRow = itemstartRow + onePageCnt
itemendRow = itemendRow + onePageCnt
itemstartRow_V2 = itemstartRow_V2 + onePageCnt
itemendRow_V2 = itemendRow_V2 + onePageCnt
Next lv_1
End If
' 3. item data 출력 하기
firstTopstartRow = 1 + topPageCnt
firstTopendRow = firstTopstartRow + firstTopPageCnt - 1
itemstartRow = firstTopendRow + 2
currPage = 1
For itemLine = 1 To r_count4
pageLine = pageLine + 1
If currPage = 1 Then
'첫 페이지
If totalPageCnt = currPage Then
If pageLine > lv_cnt_1 Then '첫 페이지가 마지막 페이지가 일 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
Else
If pageLine > lv_cnt_2 Then '첫 페이지가 마지막 페이지가 아닐 경우
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
ElseIf totalPageCnt = currPage Then
' 마지막 페이지
Else
' 기타 페이지
If pageLine > lv_cnt_3 Then
pageLine = 1
currPage = currPage + 1
itemstartRow = itemstartRow + bottomPageCnt + topPageCnt + 1
End If
End If
'==========> 여기서 item을 출력 하면 됩니다. ==========<
Range("D" & itemstartRow & ":" & "G" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("H" & itemstartRow & ":" & "K" & itemstartRow).Select
Selection.Merge '2,3,4cell을 cell병합처리 하기
Range("A" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlLeft
Range("H" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.HorizontalAlignment = xlRight
ThisWorkbook.Sheets(4).Cells(itemstartRow, 2) = r3table_4.Value(itemLine, 1) '
ThisWorkbook.Sheets(4).Cells(itemstartRow, 4) = r3table_4.Value(itemLine, 2) '
ThisWorkbook.Sheets(4).Cells(itemstartRow, 8) = r3table_4.Value(itemLine, 3) '
'ThisWorkbook.Sheets(4).Cells(itemstartRow, 9) = r3table_4.Value(itemLine, 4) '
ThisWorkbook.Sheets(4).Cells(itemstartRow, 13) = r3table_4.Value(itemLine, 5) '
Range("H" & itemstartRow & ":" & "M" & itemstartRow).Select
Selection.NumberFormatLocal = "#,##0.00"
itemstartRow = itemstartRow + 1
Next itemLine
' 3. last 바닥글의 위치를 조정 하기
' item의 마지막 라인
' itemEndLine = itemstartRow + 3
' Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
' Selection.Copy
' Cells(itemEndLine, 1).Select
' ActiveSheet.Paste
'전에 있던 last 바닥글을 지우기
' Range("A" & last_LastStaLine & ":" & "M" & last_LastEndLine).Select
' Selection.Clear
' 4. 프린트 페이지를 지정하기
Dim plt As Long
Dim pageBreakCntV2 As Long
Dim hpPlt As Long
plt = totalPageCnt * onePageCnt '최종 페이지 라인
' 4-1 최종 페이지 지정
ThisWorkbook.Sheets(4).PageSetup.PrintArea = "A1:" & dispCell & plt '최종 페이지의 프린트 영역지정
' 4-2 페이지 분할 개수
pageBreakCntV2 = ThisWorkbook.Sheets(4).HPageBreaks.Count
' 4-3 페이지 분할 위치 지정 하기
For hpact = 1 To pageBreakCntV2
' 분할 페이지 이치
page_line = onePageCnt * hpact
hpPlt = page_line + 1 ' -1 프린트 페이지 수
' 분할 페이지 라인이 부족 시 분할 라인 추가
If hpact > pageBreakCntV2 Then
ThisWorkbook.Sheets(4).Cells(hpPlt, 1).Select
ThisWorkbook.Sheets(4).HPageBreaks.Add Before:=ActiveCell ' 프린트 페이지 삽입
End If
' 최종 분할 라인 위치를 를 지정
If hpact <> totalPageCnt Then
Application.ScreenUpdating = False
ActiveWindow.View = xlPageBreakPreview
Set ActiveSheet.HPageBreaks(hpact).Location = Range("A" & hpPlt)
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
'Set ThisWorkbook.Sheets(4).HPageBreaks(hpact).Location = ThisWorkbook.Sheets(4).Range("A" & hpPlt)
End If
Next hpact
' 5. 페이지 / 총 페이지 출력
dispPageRow = 10 '2 'Page Row(시작)
dispPageCell = 9 '14 'Page Column
For L1 = 1 To totalPageCnt
Cells(dispPageRow, dispPageCell).Select
pagenumber '페이지 넘버 출력
dispPageRow = dispPageRow + onePageCnt
Next L1
If r3table_1.Value(1, 27) <> "" Then 'PDF Check
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
r3table_1.Value(1, 27), Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
Sub pagenumber()
Dim xVPC As Integer
Dim xHPC As Integer
Dim xVPB As VPageBreak
Dim xHPB As HPageBreak
Dim xNumPage As Integer
xHPC = 1
xVPC = 1
If ActiveSheet.PageSetup.Order = xlDownThenOver Then
xHPC = ActiveSheet.HPageBreaks.Count + 1
Else
xVPC = ActiveSheet.VPageBreaks.Count + 1
End If
xNumPage = 1
For Each xVPB In ActiveSheet.VPageBreaks
If xVPB.Location.Column > ActiveCell.Column Then Exit For
xNumPage = xNumPage + xHPC
Next
For Each xHPB In ActiveSheet.HPageBreaks
If xHPB.Location.Row > ActiveCell.Row Then Exit For
xNumPage = xNumPage + xVPC
Next
ActiveCell = " Page " & xNumPage & " of " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
End Sub
帮我优化代码