Page 1 of 1

การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี

Posted: Sat Feb 18, 2017 7:58 pm
by rich37
การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี ให้พิมพ์ออกทั้งหมด ตามCode ด้านล่างกำหนดไว้ที่หน้าที่ 1-1 จะเขียนแบบไหนให้ออกรายงานได้ตามจริง โดยไม่ต้องมากำหนดหน้าเอกสารครับ

Code: Select all

Sub Border_line()
Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("A3:L1048576").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Range("A1048576").End(xlUp).Select
    Range("A2:L2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    Range("A2:L2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate _ 
        :=True, IgnorePrintAreas:=False  'แถวสั่งเลือก 1-1
End Sub

Re: การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี

Posted: Sat Feb 18, 2017 8:58 pm
by snasui
:D หากต้องการพิมพ์เฉพาะที่ใช้งานสามารถใช้ UsedRange เข้ามาช่วยได้ครับเช่น Sheets("Data").UsedRange.PrintOut

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

ควรจะเลือกเฉพาะที่มีข้อมูลแล้วค่อยจัดรูปแบบแล้วค่อยพิมพ์ การจะพิมพ์เฉพาะข้อมูลจะต้องหาให้ได้ว่าบรรทัดสุดท้ายของข้อมูลอยู่ที่ใด เช่นหาบรรทัดสุดท้ายในคอลัมน์ A จะได้เป็น เช่น

Code: Select all

Dim rngAll as Range
With Sheets(1)
	Set rngAll = .Range("a3",.Range("a" & .Rows.Count).End(xlUp))
	rngAll.Resize(,12).PrintOut 'ขยายข้อมูลออกไป 12 คอลัมน์แล้วค่อยสั่งพิพม์
End With

Re: การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี

Posted: Sat Feb 18, 2017 9:15 pm
by rich37
snasui wrote::D หากต้องการพิมพ์เฉพาะที่ใช้งานสามารถใช้ UsedRange เข้ามาช่วยได้ครับเช่น Sheets("Data").UsedRange.PrintOut

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

ควรจะเลือกเฉพาะที่มีข้อมูลแล้วค่อยจัดรูปแบบแล้วค่อยพิมพ์ การจะพิมพ์เฉพาะข้อมูลจะต้องหาให้ได้ว่าบรรทัดสุดท้ายของข้อมูลอยู่ที่ใด เช่นหาบรรทัดสุดท้ายในคอลัมน์ A จะได้เป็น เช่น

Code: Select all

Dim rngAll as Range
With Sheets(1)
	Set rngAll = .Range("a3",.Range("a" & .Rows.Count).End(xlUp))
	rngAll.Resize(,12).PrintOut 'ขยายข้อมูลออกไป 12 คอลัมน์แล้วค่อยสั่งพิพม์
End With
การสั่ง พิมพ์รายงานจะใช้แค่ชีต Report แค่ชีตเดียวครับ

Re: การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี

Posted: Sat Feb 18, 2017 9:24 pm
by snasui
:D ชีตใดก็ควรที่กำหนดพื้นที่การพิมพ์เช่นที่ผมแจ้งไปครับ Code นั้นเป็นการยกตัวอย่างเท่านั้น สามารถปรับเป็นชีตที่ใช้จริงได้ตามสะดวกครับ

การทำตัวอย่างควรจะมีข้อมูลตัวอย่างมาด้วยโดยไม่จำเป็นต้องเป็นข้อมูลจริง แล้วอธิบายมาว่าจากตัวอย่างที่เห็นอยู่นั้น ต้องการให้พิมพ์แล้วออกมามีลักษณะเป็นอย่างไร จะได้เข้าใจตรงกันครับ

Re: การสั่งพิมพ์โดยเขียน vba ให้เลือกตามข้อมูลที่มี

Posted: Sat Feb 18, 2017 9:46 pm
by rich37
snasui wrote::D ชีตใดก็ควรที่กำหนดพื้นที่การพิมพ์เช่นที่ผมแจ้งไปครับ Code นั้นเป็นการยกตัวอย่างเท่านั้น สามารถปรับเป็นชีตที่ใช้จริงได้ตามสะดวกครับ

การทำตัวอย่างควรจะมีข้อมูลตัวอย่างมาด้วยโดยไม่จำเป็นต้องเป็นข้อมูลจริง แล้วอธิบายมาว่าจากตัวอย่างที่เห็นอยู่นั้น ต้องการให้พิมพ์แล้วออกมามีลักษณะเป็นอย่างไร จะได้เข้าใจตรงกันครับ
ขอบคุณครับอาจารย์เอาไปปรับใช้งานได้อย่างที่ต้องการเลยครับ