Scenario: I am building a code that reads some inputs from a worksheet, find the equivalents in other two worksheets and, if the data is available, performs some operations with it.
Data:
Worksheet 1 (CAs):
name ident date value1 value2
C xxx xx/xx/xx 5000 100
T YYY xx/xx/xx 1000 101
and so on, there are more than 2 thousand identifiers (ident) in sheets A and B, while in sheet CAs only 10 of those appear.
Worksheet 2 (A): Monthly dates
Ident 1 Ident 2 ... Ident N row number
20/01/01 1 1 1 2
20/02/01 1 1 1 3
20/03/01 1 1 1 4
...
20/12/12 1 1 1 N-1
col number 2 3 N N
Worksheet 3 (B): Daily dates
Ident 1 Ident 2 ... Ident N row number
01/01/01 1 1 1 2
02/01/01 1 1 1 3
03/01/01 1 1 1 4
...
12/12/12 1 1 1 N-1
col number 2 3 N N
What the code does: It reads sheet 1, get date and identifier, uses a lookup procedure to find the coordinates of that ident and date in the other sheets and gets the data at the intersection point.
Problem: For some reason, when I input an identifier for my IsInArray function to read, it always return False, despite it surely being in the identifiers arrays.
Question: Any ideas what I am doing wrong here?
Code:
Sub Calculations()
Dim lrow As Long, i_number As String, lastRow As Long, lastCol As Long, datefinalmatch As Long, datefinalmatch2 As Long, z1 As Long, ifinalmatch As Long
Dim lastColLetter As String, a As String
Dim p_number As Variant, amount_number As Variant, aaa As Long, bbb As Long
Dim date_number As Date
Dim wb As Workbook
Dim ilist As Variant
Set wb = ThisWorkbook
For lrow = 2 To wb.Sheets("CAs").UsedRange.Rows.count
i_number = wb.Sheets("CAs").Range("B" & lrow).Value
date_number = wb.Sheets("CAs").Range("C" & lrow)
p_number = wb.Sheets("CAs").Cells(lrow, 5)
amount_number = wb.Sheets("CAs").Range("D" & lrow)
lastRow = wb.Sheets("A").Cells(Rows.count, 2).End(xlUp).Row
lastCol = wb.Sheets("A").Cells(2, Columns.count).End(xlToLeft).Column
lastColLetter = Col_Letter(lastCol)
ilist = wb.Sheets("B").Range("B1:" & lastColLetter & "1")
datefinalmatch = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("AMT").UsedRange, lastCol, True) + 1 'row
If IsInArray(i_number, ilist) = True Then
ifinalmatch = Application.HLookup(i_number, wb.Sheets("A").UsedRange, lastRow, False) 'column
'other sheets date row finder
datefinalmatch2 = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("B").UsedRange, lastCol, True) + 1 'row
End If
Next lrow
End Sub
Private Function IsInArray(valToBeFound As String, arr As Variant) As Boolean
Dim element As Variant
For Each element In arr
If element = valToBeFound Then
IsInArray = True
Exit Function
End If
Next element
End Function
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
OBS: The code runs smoothly with no errors. When running it line by line, I see that the function is always returning false, and that is prompting the next row and eventually the end of the code, without it making any actual changes (with I omitted for brevity purposes)
OBS2: There are some variables that are declared but not used. They are used for the calculations in another part of the code. That is why I removed the Option Explicit from the top.