Page 1 of 1

สอบถามการใช้ VBA กำหนดSetPrintArea ครับ

Posted: Sun May 15, 2022 6:44 pm
by thanadul0816
สอบถามการ SetPrintArea ครับ
จากไฟล์ ผมต้องการสั่งบันทึกPDF ตาราง ในชีท รายงาน
แต่จากข้อมูลที่รายงานออกมา มีไม่เท่ากัน
ผมต้องการตัดตารางส่วนที่ว่างออก ก่อนพิมพ์
ผมลองเขียนโค้ด สองแบบ ครับ
สั่งบันทึก PDF ได้แล้ว ติดปัญหาไม่ตัดตารางส่วนที่ว่าง ครับ

Code: Select all

Sub Printrang()
 Dim Print_Area As String
    Sheets("รายงาน").Select
    Print_Area = Range("b1", Range("b" & Rows.Count).End(xlUp).Offset(0, 6)).Address
    ActiveSheet.PageSetup.PrintArea = Print_Area
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ThisWorkbook.Path & "\" & Range("F1").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, OpenAfterPublish:=True
End Sub

Code: Select all

Sub SetPrintAreaSetPrintAreaBeforePrint()
Dim i As Integer
Dim lng As Long
lng = Rows.Count
i = Range("B" & lng).End(xlUp).Row
    ActiveSheet.PageSetup.PrintArea = "$A$2:$G$" & i
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ThisWorkbook.Path & "\" & Range("F1").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, OpenAfterPublish:=True
End Sub

Re: สอบถามการใช้ VBA กำหนดSetPrintArea ครับ

Posted: Mon May 16, 2022 7:02 am
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
Dim lng As Long
lng = Rows.Count
i = Range("B" & lng).End(xlUp).Row
Do
    i = i - 1
Loop Until Range("b" & i).Value <> ""

ActiveSheet.PageSetup.PrintArea = "$A$2:$G$" & i
'Other code

Re: สอบถามการใช้ VBA กำหนดSetPrintArea ครับ

Posted: Mon May 16, 2022 11:18 am
by thanadul0816
snasui wrote: Mon May 16, 2022 7:02 am :D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
Dim lng As Long
lng = Rows.Count
i = Range("B" & lng).End(xlUp).Row
Do
    i = i - 1
Loop Until Range("b" & i).Value <> ""

ActiveSheet.PageSetup.PrintArea = "$A$2:$G$" & i
'Other code
:D :D ขอบคุณครับ ได้ผลลัพธ์ตามต้องการครับ

ขอถามเพิ่มเติมครับ
ในไฟล์ตัวอย่างเดิม
ผมใช้รูปภาพเพื่อพิมพ์ซ้ำในท้ายตารางของทุกๆหน้า เนื่องจากรูปแบบ และความยาวของข้อความ จึงต้องใช้รูปภาพ
สอบถามว่า พอมีแนวทางอื่นในการพิมพ์ซ้ำท้ายตาราง ที่ไม่ต้องใช้รูปเป็นท้ายตารางไหมครับ

Re: สอบถามการใช้ VBA กำหนดSetPrintArea ครับ

Posted: Mon May 16, 2022 11:30 am
by snasui
:D Excel ไม่มี Option การพิมพ์ซ้ำท้ายตาราง การเลือกใช้ภาพจึงเป็นทางเลือกที่ดีแล้วครับ

Re: สอบถามการใช้ VBA กำหนดSetPrintArea ครับ

Posted: Mon May 16, 2022 3:26 pm
by thanadul0816
snasui wrote: Mon May 16, 2022 11:30 am :D Excel ไม่มี Option การพิมพ์ซ้ำท้ายตาราง การเลือกใช้ภาพจึงเป็นทางเลือกที่ดีแล้วครับ
ขอบคุณครับ