TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 250
- Office Version
- 365
- 2021
- Platform
- Windows
Hi all,
My code does this:
Some example code is here:
Establishing my arrays:
Allocating the name of the necessary variants to a variable:
Calling my function:
And the function itself:
In the Locals window when the function is activated, "SearchArray" is just called "Purch" (or "Sales") and has the type Variant/String and so I get a Type Mismatch error.
Whereas if I specify the name of the Array without using the variable, I get the full array transferred into the function.
I could get around this by using IF statements but out of curiosity, is there a way to do this using the variable name instead?
My code does this:
- Populates 4 different arrays with data from locations around the workbook
- Depending on the values in ranges, picks one of these arrays to work on
- Uses a function where the name of the array is passed to that function through its parameters.
Some example code is here:
Establishing my arrays:
VBA Code:
Sales = WBk2.Sheets("Sales").UsedRange
RemSales = WBk2.Sheets("Sales-Removed").UsedRange
Purch = WBk2.Sheets("Purchases").UsedRange
RemPurch = WBk2.Sheets("Purchases-Removed").UsedRange
TabHeads = WS2.ListObjects(1).HeaderRowRange
Allocating the name of the necessary variants to a variable:
VBA Code:
If WS3.Range("RepDirection") = "A" Then
Tab1 = "Purch"
Tab2 = "RemPurch"
Else
Tab1 = "Sales"
Tab2 = "RemSales"
End If
Calling my function:
VBA Code:
IntMatch = WhereInArray(Table(Counter, IntVouch), SIIVouch, Tab1)
And the function itself:
VBA Code:
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
In the Locals window when the function is activated, "SearchArray" is just called "Purch" (or "Sales") and has the type Variant/String and so I get a Type Mismatch error.
Whereas if I specify the name of the Array without using the variable, I get the full array transferred into the function.
I could get around this by using IF statements but out of curiosity, is there a way to do this using the variable name instead?