当我更改ComboBox2值时,UserForm上的文本框不会刷新

时间:2010-09-04 10:43:46

标签: excel-vba vba excel

这是代码,我在这个表单上有2个ComboBox和2个TextBox,并且由于一些不明原因,当我更改debtor ComboBox时它会刷新余额,但不会刷新Price;价格是债务人和数量的交叉参考。

Private Sub UserForm_Initialize()

    Purchase_Select_Debtor.List = Workbooks("New Template.xlsm").Worksheets("Debtor_list").Range("A2:A13").Value
    Purchase_Select_Debtor.ListIndex = 0

    Purchase_Select_Quantity.List = Workbooks("New Template.xlsm").Worksheets("RangeNames").Range("A15:A20").Value
    Purchase_Select_Quantity.ListIndex = 0

End Sub

Private Sub cmdBuy_Purchase_Click()

    Purchase_Select_Debtor.Value = Name
        Purchase_Select_Price.Value = Amount
    Purchase_Select_Balance.Value = Balance

    If Name = "" Then
        MsgBox "Select Debtor"
        Exit Sub
    End If

    DebtorRow = 1
    Do
        TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
        If TempName = Name Then
            DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
            Exit Do
        End If
        DebtorRow = DebtorRow + 1
    Loop Until TempName = ""

    If TempName = "" Then
        MsgBox "Debtor not found"
        Exit Sub
    End If


    Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance - Amount

MsgBox "You have just Purchased " & Amount & " For $" & Amount & vbCrLf & "Your Account Balance is now: " & Balance

End Sub

Private Sub cmdClose_Purchase_Click()
  Unload Me
End Sub


Private Sub Purchase_Select_Debtor_Change()

Purchase_Select_Balance.Value = "$" & Application.VLookup(Purchase_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0)

End Sub

Private Sub Purchase_Select_Quantity_Change()

Purchase_Select_Price.Value = "$" & Application.Index(Sheets("Inventory_list").Range("A1:G13"), Application.Match(Purchase_Select_Debtor.Value, Sheets("Inventory_list").Range("A1:A13"), 0), Application.Match(Purchase_Select_Quantity.Value, Sheets("Inventory_list").Range("A1:G1"), 0))

End Sub

1 个答案:

答案 0 :(得分:1)

如果您希望在更改债务人时刷新价格和余额,则:

Private Sub Purchase_Select_Debtor_Change() 

   Purchase_Select_Balance.Value = "$" & Application.VLookup(Purchase_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0) 

   Purchase_Select_Price.Value = "$" & Application.Index(Sheets("Inventory_list").Range("A1:G13"), Application.Match(Purchase_Select_Debtor.Value, Sheets("Inventory_list").Range("A1:A13"), 0), Application.Match(Purchase_Select_Quantity.Value, Sheets("Inventory_list").Range("A1:G1"), 0)) 

End Sub