標題:
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
試試看
Private Const Sh = "DATA" 'Const 陳述式 宣告常數 , 其值如字面所示 ("DATA")
Dim D As Object
Private Sub UserForm_Initialize()
Dim i As Integer
Set D = CreateObject("Scripting.Dictionary")
With ListBox1
.ColumnCount = 4 '欗位數
.ColumnWidths = "120 pt;80 pt;80 pt;80 pt" '設定欄寬
End With
With Sheets(Sh)
i = 2
Do While .Cells(i, "A") <> ""
D(.Cells(i, "A").Value) = ""
i = i + 1
Loop
End With
With ComboBox1
.List = D.KEYs
.Value = .List(0)
End With
End Sub
Private Sub ComboBox1_Change()
Dim i As Integer, R As Integer
ListBox1.Clear
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
Set D = CreateObject("Scripting.Dictionary")
With Sheets(Sh)
i = 2
Do While .Cells(i, "A") <> ""
If .Cells(i, "A") = ComboBox1 Then
With ListBox1
.AddItem
R = .ListCount
.List(R - 1, 0) = Sheets(Sh).Cells(i, "B") '單號+序號
.List(R - 1, 1) = Sheets(Sh).Cells(i, "C") '品名
.List(R - 1, 2) = Sheets(Sh).Cells(i, "D") '規格
.List(R - 1, 3) = Sheets(Sh).Cells(i, "E") '金額
End With
End If
i = i + 1
Loop
End With
End Sub
Private Sub ListBox1_Change() '將選取ListBox的值放到TextBox
Dim Ar(1 To 3) As String, i As Integer
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Ar(1) = IIf(Ar(1) = "", "", Ar(1) & vbTab) & ListBox1.List(i, 1) '品名
Ar(2) = IIf(Ar(2) = "", "", Ar(2) & vbTab) & ListBox1.List(i, 2) '規格
Ar(3) = Val(Ar(3)) + Val(ListBox1.List(i, 3)) '金額
End If
Next
TextBox1 = Ar(1)
TextBox2 = Ar(2)
TextBox3 = Ar(3)
End Sub
複製代碼
作者:
yliu
時間:
2013-7-13 15:15
回復
2#
GBKEE
非常感謝 GBKEE .
可以使用了 ,謝謝你~
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)