Page 1 of 1

สอบถามเรื่องเลือกช่วงข้อมูลเพื่อตั้งPrint Area และสั่งปริ้นให้อยู่ในแผ่นเดียว

Posted: Thu Jul 06, 2017 11:45 pm
by Sirinat.d
ดิฉันต้องการที่จะเลือกช่วงข้อมูลที่มีCodeเดียวกัน ให้ปริ้นในแผ่นเดียวกัน ในไฟล์ตัวอย่างช่วงข้อมูลแรกที่จะปริ้นคือ A3:T24 ช่วงข้อมูลที่สองคือ A26:T61 ไม่ทราบว่าจะใช้วิธีใด ตัวอย่างตามไฟล์แนบค่ะ โดยข้อมูลจริงที่ดิฉันจะนำไปใช้จริงมีประมาณ100,000 บรรทัดค่ะ

Re: สอบถามเรื่องเลือกช่วงข้อมูลเพื่อตั้งPrint Area และสั่งปริ้นให้อยู่ในแผ่นเดียว

Posted: Fri Jul 07, 2017 1:42 am
by DhitiBank
ลองปรับโค้ดแบบนี้ดูครับ

Code: Select all

Private Sub test()
    Dim LastRow As Long, l As Long, _
        rCode As Range, r As Range, sArea As String
    With ActiveSheet
        LastRow = .Range("a" & .Rows.Count).End(xlUp).Row
        Set rCode = .Range("a1:a" & LastRow).SpecialCells( _
            xlCellTypeConstants, xlTextValues)
        l = rCode.Range("a1").End(xlToRight).Column
        sArea = rCode.Range("a1").Resize( _
                LastRow - rCode.Range("a1").Row + 1, l).Address
        With .PageSetup
            .Orientation = xlLandscape
            .PrintArea = sArea
        End With
    End With
    l = 1
    For Each r In rCode
        If r.Address <> rCode.Range("a1").Address Then
            Set ActiveSheet.HPageBreaks(l).Location = r
            l = l + 1
        End If
    Next r
End Sub