2010-09-02 267 views
0

我一直在嘗試使VBA代碼執行以下操作。替換VLOOKUP公式返回的值

我有2個表,一個名爲Pay_balance,另一個名爲Debtor_list。 Debtor_list表是來自單元格A2:B13的表格; A1 & B1是標題(「債務人」和「餘額」)。兩列都分配了名稱(「Alldebtor」 - A2:A13 &「Allbalance」B2:B13)。在Pay_balance表中,我有一個下拉列表,使用F18上的數據有效性(來源)「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和顯示味精盒,我需要的是爲它更換由VLOOKUP返回的值「Debtor_list」表中的公式爲客戶新餘額,所以當我返回到「Pay_balance」工作表時,從F18中選擇人名,它會在H18中顯示他們的新餘額爲「mycount」。

回答

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