Page 1 of 1

ตีเส้นตารางตามจำนวนข้อมูลที่แสดง

Posted: Thu Sep 26, 2013 12:19 pm
by pongpang
ต้องการให้ตีเส้นตารางตามจำนวนข้อมูลที่แสดง โดยเมื่อเลือกคอมโบบ๊อก จะมีข้อมูลแสดงออกมาและต้องการให้มีเส้นตารางข้อมูลที่แสดงออกมา ครับ
ได้เขียน โค๊ด ดังนี้

Code: Select all

Private Sub ComboBox1_Change()
    Dim irRange As Range
    Dim lr As Long
            lr = Range("e" & Rows.Count).End(xlUp).Row
            Set irRange = .Range("e6:e" & lr)
            irRange.Borders.LineStyle = xlContinuous
            irRange.Sort Key1:=.Range("e6"), Order1:=xlAscending, Header:=xlGuess
End Sub
จะเออเร่อ ตามภาพที่แนบ ครับ สำหรับไฟล์ตัวอย่างได้แนบมาด้วยแล้วครับ ไม่ทราบว่าจะต้องปรับปรุงหรือแก้ไขโค๊ดอย่างไร ครับ

Re: ตีเส้นตารางตามจำนวนข้อมูลที่แสดง

Posted: Thu Sep 26, 2013 4:02 pm
by niwat2811
ลองแบบนี้ว่าใช้ได้ไหมครับ

Code: Select all

Private Sub ComboBox1_Change()
Dim ra As Range, r As Range
Dim lr As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
With Range("E6:E" & lr)
    .ClearContents
    .Borders.LineStyle = xlNone
End With
With Sheets("Sheet1")
    Set ra = .Range("I5", .Range("I" & Rows.Count).End(xlUp))
End With
For Each r In ra
    If r = Sheets("Sheet1").OLEObjects("ComboBox1").Object.Text Then
        Sheets("Sheet1").Range("E" & Rows.Count).End(xlUp).Offset(1, 0) = r.Offset(0, -1)
    End If
Next r
lr = Range("E" & Rows.Count).End(xlUp).Row
With Range("E6:E" & lr)
    .Borders.LineStyle = xlContinuous
End With
End Sub

Re: ตีเส้นตารางตามจำนวนข้อมูลที่แสดง

Posted: Thu Sep 26, 2013 4:24 pm
by pongpang
ขอบคุณ คุณ niwat2811 มากครับ