返回列表 上一主題 發帖

[發問] ListBox1.ColumnCount = 2時,如何AddItem?如何取出?

[發問] ListBox1.ColumnCount = 2時,如何AddItem?如何取出?

請教各位大大
     Dim d As Object, a, b
    Set d = CreateObject("Scripting.Dictionary")
    With .Sheets("基本資料")
                .Visible = True
                For i = 1 To .Cells(65536, 4).End(xlUp).Row
                    d(.Cells(i, 4).Text) = .Cells(i, 5)
                Next
    End  With
    a = d.Keys
    b = d.Items
   ListBox1.ColumnCount = 2
問題是:
1.AddItem時   如何使    ListBox1.List (column(1) )= d.Keys  而  column(2) AddItem是  d.Items
          ListBox1.List= d.Keys  僅是column(1)
2 當  ListBox1_Click()
   如何使        TextBox1 = ListBox1.Value(第一個column)
                       TextBox2= ListBox1.Value(第二個column)
懇請指導    謝謝

本帖最後由 yangjie 於 2014-12-8 15:22 編輯

回復 2# GBKEE
謝謝
不用Dictionary 而達到目的,我受教了。
For i = 1 To .Cells(65536, 4).End(xlUp).Row
            ListBox1.AddItem
            a = ListBox1.ListCount - 1  
            ListBox1.List(a, 0) = .Cells(i, 4)
            ListBox1.List(a, 1) = .Cells(i, 5)
Next
其中
a = ListBox1.ListCount - 1  可能得到 -1值
如何避免?

TOP

回復 2# GBKEE
請教GBKEE版大
因listbox1用additem增加item而不是用.referto作list
(因為資料來源有選擇性(可能是第3,4,8,12,13,14,18.....列)
故如何設定或令使第一列listitem不動,(scrollbars轉動時)
也就是說將第一列設定為標頭columnheads(或類似)
敬請指導  謝謝

TOP

本帖最後由 yangjie 於 2014-12-24 17:31 編輯

回復 6# GBKEE
謝謝GBKEE版大
ListBox2.Clear
    wb1.Activate
    With Sheets("新生資料")
        If TextBox11 = 0 Then
            row1 = .Cells(65536, 5).End(xlUp).Row
            If row1 < 2 Then Exit Sub
            Dim mystr As String
            With Application.Names.Add("Tmp", .Range(.Cells(2, 1), .Cells(row1 - 5, 11)))
                mystr = .RefersTo
                .Delete
            End With
            With ListBox2
                .ColumnCount = 11
                .RowSource = mystr
                .ColumnHeads = True
            End With
'要加入.Range(.Cells(row1 - 1, 1), .Cells(row1 - 1, 11))
            With ListBox2
                .AddItem
                .RowSource = .Range(.Cells(row1 - 1, 1), .Cells(row1 - 1, 11))
            End With
      End With
偵錯說:沒有此成員或....無法加入 或移除部分
另有一問題  當ListBox2_Click
MsgBox ListBox2.List(ListIndex, 3)
MsgBox ListBox2.List(ListIndex, ?)如何可找出該列的 Row?

TOP

        靜思自在 : 時時好心就是時時好日。
返回列表 上一主題