
Ag
-
界面跨框架搭建,將列表框放入框架中,借助框架的好處是移動(dòng)時(shí)可以一起移動(dòng)
效果框
2.展示第一個(gè)框
3.展示第二和第三個(gè)框
邏輯:點(diǎn)擊第一個(gè)框時(shí),第二框會被展示,第三個(gè)框同理
完整代碼
Dim arr()
Dim ID As String
Dim price As Long
Private Sub ListBox4_Click()
End Sub
Private Sub UserForm_Activate()
Dim dic As New Dictionary
arr = Sheet2.Range("a2:e" & Sheet2.Range("a65536").End(xlUp).Row)
For i = LBound(arr) To UBound(arr)
dic(arr(i, 2)) = 1
Next
'1.展示第一個(gè)框'
Me.ListBox1.List = dic.Keys
End Sub
Private Sub ListBox1_Click()
'點(diǎn)擊第一個(gè)框時(shí)展示第二個(gè)框'
Dim dic As New Dictionary
For i = LBound(arr) To UBound(arr)
If arr(i, 2) = Me.ListBox1.Value Then
dic(arr(i, 3)) = 1
End If
Next
Me.ListBox2.List = dic.Keys
'點(diǎn)擊第一個(gè)框時(shí)清空第三個(gè)框'
Me.ListBox3.Clear
'價(jià)格也需清空為0'
Me.Label2.Caption = 0
End Sub
Private Sub ListBox2_Click()
'點(diǎn)擊第二框時(shí)展示第三個(gè)框
'
Dim dic As New Dictionary
For i = LBound(arr) To UBound(arr)
If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value Then
dic(arr(i, 4)) = 1
End If
Next
Me.ListBox3.List = dic.Keys
'價(jià)格清空為0'
Me.Label2.Caption = 0
End Sub
Private Sub ListBox3_Click()
'點(diǎn)擊到第三個(gè)框時(shí)顯示單價(jià)'
For i = LBound(arr) To UBound(arr)
If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value And arr(i, 4) = Me.ListBox3.Value Then
ID = arr(i, 1)
price = arr(i, 5)
End If
Next
Me.Label2.Caption = price
End Sub
Private Sub CommandButton1_Click()
'點(diǎn)擊選擇按鈕是將所選的商品放到購物籃'
'開始前加個(gè)判斷,如果列表框中沒東西則不執(zhí)行'
If Me.ListBox1.Value <> "" And Me.ListBox2.Value <> "" And Me.ListBox3.Value <> "" And Me.TextBox1.Value > 0 Then
'增加一個(gè)空行行'
Me.ListBox4.AddItem
'0行0列相當(dāng)于第一行第一列'
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = ID
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Me.ListBox1.Value
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = Me.ListBox2.Value
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = Me.ListBox3.Value
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = Me.TextBox1.Value
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Me.TextBox1.Value * Me.Label2.Caption
Else
MsgBox "請正確選擇商品"
End If
Private Sub CommandButton2_Click()
'刪除商品'
'遍歷時(shí)時(shí)從0開始'
For i = 0 To Me.ListBox4.ListCount - 1
If Me.ListBox4.Selected(i) = True Then
'刪除后減去對應(yīng)的價(jià)格'
Me.Label5.Caption = Me.Label5.Caption - Me.ListBox4.List(i, 5)
'移除刪除后的這一行'
Me.ListBox4.RemoveItem i
End If
Next
End Sub
Private Sub CommandButton3_Click()
'點(diǎn)擊結(jié)算按鈕時(shí)進(jìn)行結(jié)算并記錄購買的數(shù)據(jù)'
'訂單ID'
Dim DDID As String
Dim i
If Me.ListBox4.ListCount > 0 Then
'往下一行寫數(shù)據(jù)'
i = Sheet2.Range("a65536").End(xlUp).Row + 1
DDID = "D" & Format(VBA.Now, "yyyymmddhhmmss")
For j = 0 To Me.ListBox4.ListCount - 1
Sheet2.Range("a" & i) = DDID
Sheet2.Range("b" & i) = Date
'取ID'
Sheet2.Range("c" & i) = Me.ListBox4.List(j, 0)
'取數(shù)量'
Sheet2.Range("d" & i) = Me.ListBox4.List(j, 4)
'取金額'
Sheet2.Range("e" & i) = Me.ListBox4.List(j, 5)
'下一次循環(huán)時(shí) i 的行數(shù)是下一行 即 i + 1'
i = i + 1
Next
MsgBox "結(jié)算成功"
'關(guān)閉窗體'
Unload Me
Else
MsgBox "購物清單為空"
End If
End Sub
End Sub


