I am trying to create an array of string variables (not an array of text strings, so to speak), with the goal of passing each variable into the following procedure.
In my actual use case, I have two large blocks of code that are exactly the same for each of two string variables, and I am really just trying to shorten / consolidate my code.
I have pasted an example of the code below.
So I get it...the test will always print "Fail" because it's essentially testing if "strA" = "Apple" and then if "strB" = "Apple", but my intent is for it to test if strA = "Apple" and then if strB = "Apple" (where strA and strB are treated as variables).
Any advice? Is what I am trying to accomplish even possible? Thank you much.
Sub ArrayTest()
'Declare variables
Dim strA As String: strA = "Apple"
Dim strB As String: strB = "Banana"
Dim strArray() As String: strArray = Split("strA,strB", ",")
Dim i As Long
Dim strResult As String
'Test the array
For i = 0 To UBound(strArray)
If strArray(i) = "Apple" Or strArray(i) = "Banana" Then
strResult = "Pass"
Else: strResult = "Fail"
End If
Debug.Print strResult
Next
End Sub
"A", "B"etc.? There is no direct way of doing what you want to do, but even if there were, it would probably be a bad decision. Seems like a classic case of an XY problem. What problem are you actually trying to solve?Sub ThatLargeBlockOfCode(byval s as string)and call itThatLargeBlockOfCode "Apple"andThatLargeBlockOfCode "Banana"?