替换VLOOKUP公式返回的值

时间:2010-09-02 14:18:51

标签: excel vba

我一直在尝试制作VBA代码来执行以下操作。

我有2张表,一张名为Pay_balance,另一张名为Debtor_list。 Debtor_list表是来自单元格A2的表:B13; A1& B1是标题(“债务人”和“余额”)。两列都分配了名称(“Alldebtor” - A2:A13&“Allbalance”B2:B13)。在Pay_balance表上我有一个下拉列表,使用F18 A Cell上的数据验证(来源)“Alldebtors”输入G18上的值和使用下面的公式在F18单元格H18中选择的“Alldebtors”客户的当前余额:

=VLOOKUP(F18,Debtor_list!A2:B13,2,FALSE)

然后我使用VBA代码获得了付费按钮;

Sub Pay_Click()

    mycount = Range("G18") + Range("H18") Range("a1") = mycount

    MsgBox "You have just Credited $" & Range("G18") & vbCrLf & "Your Account Balance is now: " & mycount

    Application.Goto Reference:="Creditbox" 
    Selection.ClearContents 

    Application.Goto Reference:="Balance_Debtor" 
    Selection.ClearContents 

    Sheets("Menu").Select 
End Sub

G18 + H18的结果是“mycount”它当前将结果粘贴到“Pay_balance”表的A1中并显示一个MSG Box,我需要的是它替换VLOOKUP公式返回的值“ Debtor_list“Sheet是客户的新余额,所以当我返回”Pay_balance“表时,从F18中选择人名,它显示他们的新余额是H18中的”mycount“。

1 个答案:

答案 0 :(得分:1)

Sub Pay_Click() 

    Name = Worksheets("pay_balance").Range("F18").Value 
    Amount = CSng(Worksheets("pay_balance").Range("G18").Value) 

    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 Credited $" & Range("G18") & vbCrLf & "Your Account Balance is now: " & Range("H18") 

    Application.Goto Reference:="Creditbox" 
    Selection.ClearContents 

    Application.Goto Reference:="Balance_Debtor" 
    Selection.ClearContents 

    Sheets("Menu").Select 

End Sub