I am trying to get the value from dbAccountRng (column D in Transaction Database) from the xlookup function, if three variable match. Heres the code:
Private Sub USBank_Click()
UserForm1.Hide
'Get bank download file
Dim bankDownload As Variant
Dim fileName As String
myFile = Application.GetOpenFilename("Excel Files (*.csv*),*.csv", , "Choose Bank Download File", "Open", False)
If myFile = False Then
Exit Sub
Else
fileName = Dir(myFile)
Workbooks.Open (myFile)
Workbooks(fileName).Worksheets(1).Copy After:=ThisWorkbook.Sheets(1)
ActiveSheet.name = "Bank Download"
Workbooks(fileName).Close
End If
'get info from bank download file
Dim entryPath As String
Dim dataPath As String
Dim bankInfoFile As String
Dim databaseFile As String
Dim yardiTemplate As String
entryPath = Application.ThisWorkbook.Path
dataPath = entryPath & "\Data"
bankInfoFile = dataPath & "\subCDE Bank Info.xlsx"
databaseFile = dataPath & "\Transaction Database.xlsx"
yardiTemplate = dataPath & "\Upload Template.csv"
Dim lastRow As Long
Dim rng As Range
Dim ws As Worksheet
Dim amount As Variant
Dim subCDE As Variant
Dim debit As Boolean
Dim tempDate As Variant
Dim tranDate As Date
Dim account As String
Dim description As String
Dim detail As String
Dim yardiCode As String
Dim bankAcct As String
Dim dbAccount As String
Dim dbExist As Boolean
Dim currentRow As Long
Set ws = ThisWorkbook.Sheets("Bank Download")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ThisWorkbook.Sheets("Bank Download").Range("A:A").Insert
Set rng = ws.Range("B2:K" & lastRow)
For i = 1 To rng.Rows.Count
amount = rng(i, "A").Value
subCDE = rng(i, "C").Value
account = rng(i, "D").Value
debit = rng(i, "E") = "Debit"
tempDate = rng(i, "F").Value
currentRow = i + 1
If Len(tempDate) = 7 Then
tranDate = Mid(rng(i, "F"), 1, 1) & "-" & Mid(rng(i, "F"), 2, 2) & "-" & Mid(rng(i, "F"), 4, 4)
Else
tranDate = Mid(rng(i, "F"), 1, 2) & "-" & Mid(rng(i, "F"), 3, 2) & "-" & Mid(rng(i, "F"), 5, 4)
End If
'ws.Range("G" & currentRow) = tranDate
Workbooks.Open databaseFile
Dim tranDateRng As Range
Dim subCDERng As Range
Dim dbAmountRng As Range
Dim dbAccountRng As Range
Set tranDateRng = Workbooks("Transaction Database.xlsx").Worksheets(1).Range("A:A")
Set subCDERng = Workbooks("Transaction Database.xlsx").Worksheets(1).Range("B:B")
Set dbAmountRng = Workbooks("Transaction Database.xlsx").Worksheets(1).Range("C:C")
Set dbAccountRng = Workbooks("Transaction Database.xlsx").Worksheets(1).Range("D:D")
dbAccount = WorksheetFunction.XLookup(1, (tranDateRange = tempDate) * (subCDERange = subCDE) * (dbAmountRng = amount), dbAccountRng, "")
'ws.Range("N" & currentRow) = "=XLOOKUP(1,('[Transaction Database.xlsx]Transactions'!$A:$A=G" & currentRow & ")*('[Transaction Database.xlsx]Transactions'!$B:$B=""" & subCDE & """)*('[Transaction Database.xlsx]Transactions'!$C:$C=" & amount & "),'[Transaction Database.xlsx]Transactions'!$D:$D,""False"")"
'dbExist = ws.Range("N" & currentRow)
'ws.Range("N" & currentRow).Clear
If InStr(UCase(rng(i, "G")), UCase("Miscellaneous Fee(s)")) > 0 Then
description = "Bank Fee"
Workbooks("Transaction Database.xlsx").Worksheets(1).Select
Workbooks("Transaction Database.xlsx").Worksheets(1).Range("A1048576").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveCell = tranDate
ActiveCell.Offset(0, 1).Select
ActiveCell = subCDE
ActiveCell.Offset(0, 1).Select
ActiveCell = amount
ActiveCell.Offset(0, 1).Select
ActiveCell = description
Else
'ws.Range("N" & currentRow) = "=XLOOKUP(1,('[Transaction Database.xlsx]Transactions'!$A:$A=G" & currentRow & ")*('[Transaction Database.xlsx]Transactions'!$B:$B=""" & subCDE & """)*('[Transaction Database.xlsx]Transactions'!$C:$C=" & amount & "),'[Transaction Database.xlsx]Transactions'!$D:$D)
'If Application.WorksheetFunction.XLookup(
End If
If debit = True And InStr(UCase(rng(i, "I")), UCase("Advisors")) > 0 Then
detail = "Management Fee"
Else
detail = "Interest?"
End If
Next i
End Sub
If the date, name, and the amount match with what is on the bank statement, i want to be able to get what is in column D of the Transaction Database excel file. Otherwise, if the transaction is not in the database, i want it to be empty/0. Im getting a type mismatch right now, once i get that figured out, then ill fix what shows up if xlookup comes up empty.
When mapping worksheet formulas into VBA, you'll find that there are things you can do in a worksheet formula which do not have an equivalent in VBA.
In those cases it's sometimes easier to fall back on using
Evaluate
to execute a worksheet formula.For example (based on your use case): how to match on 3 columns of a data table and return the matched row number.
Note whether you need quotes around the lookup values in the formula will depend on the type of data in the columns used for each value - eg. if numeric then there's no need for quotes.
My test data table: