Page 1 of 1

การแสดงผลของ ListBox

Posted: Fri Apr 18, 2014 5:12 pm
by diamondbom
คือว่าผมต้องการเขียนโค้ดให้แสดงผลใน บรรทัดเดียวกัน
แต่ไม่รู้วิธี รบกวนด้วยครับ
_ขอบคุณครับ_

Re: การแสดงผลของ ListBox

Posted: Fri Apr 18, 2014 8:32 pm
by tupthai
ลองทดสอบดูครับ

Code: Select all

Dim Y As Integer,Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With Sheet1.LB1
       .Clear
       .ColumnCount = 2
       For Y = 0 To LR - 1
          .AddItem
          .Column(0, Y) = Cells(Y + 1, 1)
          .Column(1, Y) = Cells(Y + 1, 2)
       Next
End With

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 1:47 am
by diamondbom
ขอบคุณครับ

ขอถามเพิ่มเติมนะครับ
ถ้าข้อมูลน้อยๆ แล้วต้องให้แสดงผลอยู่บรรทัดเดียวกัน
ทำอย่างไรครับ

ขอบคุณครับ :D

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 1:34 pm
by snasui
:D ลองดูตัวอย่างการปรับ Code ตามด้านล่างครับ

Code: Select all

Private Sub CommandButton1_Click()
    Dim Y As Integer, LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    With Sheet1.LB1
        .Clear
        .ColumnCount = 3
        For Y = 1 To LR
            For i = 0 To 2
                .AddItem
                .Column(i, Y - 1) = Cells(Y, i + 1)
            Next i
        Next Y
    End With
End Sub

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 3:25 pm
by diamondbom
ขอบคุณครับ :thup:

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 4:40 pm
by diamondbom
ขอถามเพิ่มเติมนะครับ :D
เราสามารถปรับระยะได้หรือเปล่าครับ
แบบ 1 AAAAAAAAAAA B
2 A B
3 AAA B


__ขอบคุณครับ_

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 5:49 pm
by diamondbom
รบกวนอีกคำถามนะครับ :D

ต้องการเลือกค่า Column B =A
แต่ติดตรงบรรทัด

.Column(0, Y) = Cells(Y + 1, 1)
.Column(1, Y) = Cells(Y + 1, 2)

ควรแก้อย่างไรครับ

__ขอบคุณครับ_

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 7:03 pm
by tupthai

Code: Select all

Private Sub CommandButton1_Click()
  Dim Y As Integer: Dim LR As Long
  Dim i As Integer
  LR = Range("A" & Rows.Count).End(xlUp).Row
  With Sheet1.LB1
       .Clear
       .ColumnCount = 3
       .ColumnWidths = "20;130;30"
       For Y = 1 To LR
          If Cells(Y, 2).Value = "A" Then
             .AddItem
             .Column(0, i) = Cells(Y, 1)
             .Column(1, i) = Cells(Y, 2)
             .Column(2, i) = Cells(Y, 3)
              i = i + 1
          End If
       Next
  End With
End Sub

Re: การแสดงผลของ ListBox

Posted: Sat Apr 19, 2014 7:16 pm
by diamondbom
ขอบคุณครับ :D