Odd behaviour from Arrays - can't find matches

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
252
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
My code does the following:

  • Loads in a range from a worksheet into an array
  • Loads in a range from another worksheet into a separate array
  • Loops through the first array in a specific column
  • Searches a column in the second array and counts how many times it appears
The code looks like this:

VBA Code:
        For Counter2 = 1 To UBound(IStat, 1)
        
            If FinalArray(Counter, a) = IStat(Counter2, b) Then Counter3 = Counter3 + 1
        
        Next Counter2

The variables a and b are used to identify the "columns" of the array and work ok.

The problem: I never get a value for Counter3 except 0.

Hovering over each expression in Step Through mode tells me that all parameters are working correctly, except that the search term in final array looks like a number (e.g. 9200) but in the search array, it is the same number inside quotation marks, so "9200". In fact, every line (including empties) in the search array is enclosed in "".

This is not returning a match.

I have tried:
  • Formatting the whole source data for both arrays as text
  • Formatting the specific columns as numbers

But so far to no avail.

Can anybody see a way around this?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi @TheRedCardinal . I hope you are well.

It is difficult to provide help, since we have to assume where your data is, how your data is, etc.

It would be great if you put a sample of your data (use the xl2bb tool)

You must also put your complete macro.

Explaining with your data shows what you have and what you expect as a result.

------------------
Or maybe, if numbers:

Rich (BB code):
If val(FinalArray(Counter, a)) = val(IStat(Counter2, b)) Then Counter3 = Counter3 + 1




--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Last edited:
Upvote 0
Hi @TheRedCardinal . I hope you are well.

It is difficult to provide help, since we have to assume where your data is, how your data is, etc.

It would be great if you put a sample of your data (use the xl2bb tool)

You must also put your complete macro.

Explaining with your data shows what you have and what you expect as a result.

------------------
Or maybe, if numbers:

Rich (BB code):
If val(FinalArray(Counter, a)) = val(IStat(Counter2, b)) Then Counter3 = Counter3 + 1




--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​

Thanks Dante Amor.

So your suggestion to use Val "worked" in the sense that it matched the numbers, but unfortunately that isn't a long term solution because I need to do other things later. So I need to understand why it is loading in data from Excel that is formatted as a number, as a string.

Here is a sample of the data (some columns removed and far fewer rows):

UK01 Intrastat - Baby Macro - Columns.xlsm
ABCDEFGH
3Document numberItemHigher-level itemBilling TypeComm./imp. code no.Invoice valueCurrencyStatistical value
438003070100RE3402399090-5646.42EUR-5646.42
592077618110F2290410009014863.5EUR14863.5
692077619110F22908990015890.6EUR15890.6
792077645210F2340242009079134EUR79134
892077649110F2340239909033637.5EUR33637.5
992077656110F2340239909021955.3EUR21955.3
1092077657110F234023990902480EUR2480
1192077675110F2340242009071614EUR71614
1292077686110F2290410009300EUR9300
1392077687110F234025010001480EUR1480
1492077702110F234023990902130EUR2130
1592077722110F22904100011264EUR11264
1692077723110F2290410009015727.5EUR15727.5
1792077724110F2290410009011600EUR1450
1892077725110F234024200904878EUR4878
1992077734110F2340231000057420EUR20880
2092077737110F2290410009020485EUR20485
2192077738110F2340239909019980EUR19980
2292077744110F2340242009014048EUR14048
2392077745110F2340231000059334EUR18879
2492077752110F2340239909026078EUR26078
2592077772110F2340239909011679EUR11679
2692077773110F234024900009180EUR9180
2792077773210F2340239909015129.6EUR15129.6
Intrastat Data
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:ACell ValueduplicatestextNO


This is the range loaded into the array IStat

And here is the data range that is loaded into the array
UK01 Intrastat - Baby Macro - Columns.xlsm
AB
3DateInvoice
401.05.202392077618
501.05.202392077619
601.05.202338003070
702.05.202392077645
802.05.202392077649
902.05.202392077656
1002.05.202392077657
1102.05.202392077675
1202.05.202392077905
1302.05.202392077930
1403.05.202392077686
1503.05.202392077687
1603.05.202392077702
1703.05.202392077737
1804.05.202392077722
1904.05.202392077723
Final Data


The column headed "document number" is formatted as "Number" yet when I load it into the array here:

VBA Code:
IStat = WS4.Range(CellA, WS4.Cells(LastRow(WS4, CellA.Column), LastCol(WS4, CellA.Row)))

That "column" in the array now becomes a string.
 
Upvote 0
I am not a VBA expert, but have you defined all the arrays as integers/long/double?
 
Upvote 0
Addendum:

I added this bit of code:

VBA Code:
'********Temporary Fix************
'**Invoices were being loaded as strings so now looking to convert**

For Counter = 2 To UBound(IStat)

    IStat(Counter, 1) = Val(IStat(Counter, 1))

Next Counter

'********End of Temporary Fix***********

This has solved the issue so far, but I'd love to understand how to avoid it in full as I may need it again later.
 
Upvote 0
Again you did not put your complete code
Could you please post all your code, that way I wouldn't have to assume anything.
Sorry Dante Amor,

Here it is:

VBA Code:
Sub MakeDispatchesTable()

Dim WS1 As Worksheet, WS2 As Worksheet, WS3 As Worksheet, WS4 As Worksheet, WS5 As Worksheet
Dim Counter As Long
Dim VATArray As Variant, FinalArray As Variant, IStat As Variant

Set Wbk = ThisWorkbook
Set WS1 = Wbk.Sheets("Final Data")
Set WS2 = Wbk.Sheets("Settings")
Set WS3 = Wbk.Sheets("Start")
Set WS4 = Wbk.Sheets("Intrastat Data")
Set WS5 = Wbk.Sheets("VAT Report")

SCols = WS2.Range("DColumns")

'Populate Intrastat Array

Set CellA = WS4.UsedRange.Find("Document number")

IStat = (WS4.Range(CellA, WS4.Cells(LastRow(WS4, CellA.Column), LastCol(WS4, CellA.Row))))

'********Temporary Fix************
'**Invoices were being loaded as strings so now looking to convert**

For Counter = 2 To UBound(IStat)

    IStat(Counter, 1) = Val(IStat(Counter, 1))

Next Counter

'********End of Temporary Fix***********

Set CellA = WS5.UsedRange.Find("Tx")

Dim a As Long

a = CellA.Column
b = WorksheetFunction.CountIf(WS5.Columns(a), "=A3") - 1
c = WorksheetFunction.Match("A3", WS5.Columns(a), 0)
d = LastCol(WS5, a)

VATArray = WS5.Range(WS5.Cells(c, 1), WS5.Cells(b + c, d))
VATHeaders = WS5.Range(WS5.Cells(CellA.Row, 1), WS5.Cells(CellA.Row, d))

ReDim FinalArray(1 To UBound(IStat, 1), 1 To UBound(SCols, 1))

'Allocate headers

For Counter = 1 To UBound(SCols, 1)

    FinalArray(1, Counter) = SCols(Counter, 1)

Next Counter
    
'Redefine the DColumns Data with Array Locations

For Counter = 1 To UBound(SCols, 1)

    If SCols(Counter, 2) <> "" Then
    
        SCols(Counter, 2) = ArrayHeader(SCols(Counter, 2), VATHeaders)
    
    ElseIf SCols(Counter, 3) <> "" Then
    
        SCols(Counter, 3) = ArrayHeader(SCols(Counter, 3), IStat)
     
    End If
    
Next Counter
    
ArrRow = 2

For Counter = 1 To UBound(VATArray, 1) - 1

    For Counter1 = 1 To UBound(SCols, 1)

        If IsEmpty(SCols(Counter1, 2)) = False Then FinalArray(ArrRow, Counter1) = VATArray(Counter, SCols(Counter1, 2))
    
    Next Counter1

ArrRow = ArrRow + 1

Next Counter

'Populate the table with final data

a = ArrayHeader("Invoice", FinalArray)
b = ArrayHeader("Document number", IStat)

For Counter = 2 To UBound(FinalArray, 1)

    Counter3 = 0
    
    If FinalArray(Counter, 1) <> "" Then
        
        'Count how many times the invoice appears in the IStat file
        
        For Counter2 = 1 To UBound(IStat, 1)
        
            If Val(FinalArray(Counter, a)) = Val(IStat(Counter2, b)) Then Counter3 = Counter3 + 1
        
        Next Counter2
        
        Select Case Counter3
            
            Case 0
                
                'Do nothing?
                
            Case 1
                
                IStatData = WhereInArray(FinalArray(Counter, a), b, IStat)
                    
                For Counter4 = 1 To UBound(SCols, 1)
                        
                    If IsEmpty(SCols(Counter4, 3)) = False Then FinalArray(Counter, Counter4) = IStat(IStatData, SCols(Counter, 3))
                       
                Next Counter4
                    
            Case Is > 1
                
                ' To figure out
                
            End Select

    End If

Next Counter

WS1.Range("A3").Resize(UBound(FinalArray, 1), UBound(FinalArray, 2)) = FinalArray

End Sub

LastRow, LastCol and WhereinArray are functions I have built:

VBA Code:
Function LastRow(WS As Worksheet, iCol)

    LastRow = WS.Cells(Rows.Count, iCol).End(xlUp).Row
    
End Function

Function LastCol(WS As Worksheet, iRow)

    LastCol = WS.Cells(iRow, Columns.Count).End(xlToLeft).Column
    
End Function

Function IsInArray(Search As Variant, Vector As Long, SearchArray As Variant) As Boolean

Dim MyCount As Long

IsInArray = False

For MyCount = LBound(SearchArray, Vector) To UBound(SearchArray, Vector)
    
    If SearchArray(MyCount, Vector) = Search Then IsInArray = True

Next MyCount

End Function

Function WhereInArray(Search As Variant, Vector As Variant, SearchArray As Variant) As Long

For MyCount = LBound(SearchArray, 1) To UBound(SearchArray, 1)

    If SearchArray(MyCount, Vector) = Search Then
        WhereInArray = MyCount
        Exit Function
    End If
    
Next MyCount

MyCount = 0

End Function
Function ArrayHeader(Search As Variant, SearchArray As Variant) As Long

For MyCount = LBound(SearchArray, 2) To UBound(SearchArray, 2)

    If SearchArray(1, MyCount) = Search Then
        ArrayHeader = MyCount
        Exit Function
    End If

Next MyCount

MyCount = 0
End Function

Function DoesSheetExist(Wbk As Workbook, SheetName As String) As Boolean

DoesSheetExist = False

For Each WS In Wbk.Worksheets

    If SheetName = WS.Name Then
        DoesSheetExist = True
        Exit Function
    End If

Next WS

End Function

Function WhereIn2DArray(Search As Variant, SearchArray As Variant) As Long

For MyCount = LBound(SearchArray) To UBound(SearchArray)

    If SearchArray(MyCount) = Search Then
        WhereIn2DArray = MyCount
        Exit Function
    End If
    
Next MyCount

MyCount = 0

End Function
 
Upvote 0
This is the range loaded into the array IStat

And here is the data range that is loaded into the array

These values appear to be text, are they really numbers in your sheet?
1686580411726.png
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top