Board logo

標題: ListBox 應用 [打印本頁]

作者: yliu    時間: 2013-7-12 23:20     標題: ListBox 應用

[attach]15410[/attach]
請問版上的高手,
我希望可以製作一個:ListBox從工作表上抓取多個欄位資料然後存放在同一列裡(如下),該如何在ListBox上點選其中一列將"清單內的第1欄+第2欄資料 TW-1301001-001 BOOK1,TW-1301001-002 BOOK2,TW-1301001-003 BOOK3”放置到TextBox1; ”清單內的第3欄A,B,C” 放置到TextBox2, 並加總金額後放置到TextBox3.
因能力有限,只能做到將清單內的第1欄放置到TextBox1(檔案詳附件), 請高手幫幫忙.感謝~

[attach]15409[/attach]
作者: GBKEE    時間: 2013-7-13 09:35

回復 1# yliu
試試看
  1. Private Const Sh = "DATA" 'Const 陳述式 宣告常數 , 其值如字面所示 ("DATA")
  2. Dim D As Object
  3. Private Sub UserForm_Initialize()
  4.     Dim i As Integer
  5.     Set D = CreateObject("Scripting.Dictionary")
  6.     With ListBox1
  7.         .ColumnCount = 4                                      '欗位數
  8.         .ColumnWidths = "120 pt;80 pt;80 pt;80 pt"            '設定欄寬
  9.     End With
  10.     With Sheets(Sh)
  11.         i = 2
  12.         Do While .Cells(i, "A") <> ""
  13.             D(.Cells(i, "A").Value) = ""
  14.             i = i + 1
  15.         Loop
  16.     End With
  17.     With ComboBox1
  18.         .List = D.KEYs
  19.         .Value = .List(0)
  20.     End With
  21. End Sub
  22. Private Sub ComboBox1_Change()
  23.     Dim i As Integer, R As Integer
  24.     ListBox1.Clear
  25.     TextBox1 = ""
  26.     TextBox2 = ""
  27.     TextBox3 = ""
  28.     Set D = CreateObject("Scripting.Dictionary")
  29.     With Sheets(Sh)
  30.         i = 2
  31.         Do While .Cells(i, "A") <> ""
  32.             If .Cells(i, "A") = ComboBox1 Then
  33.                 With ListBox1
  34.                     .AddItem
  35.                     R = .ListCount
  36.                     .List(R - 1, 0) = Sheets(Sh).Cells(i, "B")  '單號+序號
  37.                     .List(R - 1, 1) = Sheets(Sh).Cells(i, "C")  '品名
  38.                     .List(R - 1, 2) = Sheets(Sh).Cells(i, "D")  '規格
  39.                     .List(R - 1, 3) = Sheets(Sh).Cells(i, "E")  '金額
  40.                 End With
  41.             End If
  42.             i = i + 1
  43.         Loop
  44.     End With
  45. End Sub
  46. Private Sub ListBox1_Change() '將選取ListBox的值放到TextBox
  47.     Dim Ar(1 To 3) As String, i As Integer
  48.     For i = 0 To ListBox1.ListCount - 1
  49.         If ListBox1.Selected(i) = True Then
  50.             Ar(1) = IIf(Ar(1) = "", "", Ar(1) & vbTab) & ListBox1.List(i, 1)    '品名
  51.             Ar(2) = IIf(Ar(2) = "", "", Ar(2) & vbTab) & ListBox1.List(i, 2)    '規格
  52.             Ar(3) = Val(Ar(3)) + Val(ListBox1.List(i, 3))                       '金額
  53.         End If
  54.     Next
  55.     TextBox1 = Ar(1)
  56.     TextBox2 = Ar(2)
  57.     TextBox3 = Ar(3)
  58. End Sub
複製代碼

作者: yliu    時間: 2013-7-13 15:15

回復 2# GBKEE

非常感謝 GBKEE .
可以使用了 ,謝謝你~




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)