Function If 2 array = value

Infine

Board Regular
Joined
Oct 16, 2019
Messages
93
Office Version
  1. 365
Platform
  1. Windows
Hello,

So I have created an array with 2 values in it SEE: KMyarray = Application.Transpose(Range(rngArray1, rngArray2))
In my other variable I have StrLookForString = <code here> (see below).

I want to see if the value in MyArray1 and the value in MyArray2 = StrLookForString .

So basically
MyArray(1) & MyArray(2) = Variable


How do I do this? See some of the code below:


VBA Code:
Set rngArray1 = Range(Sheets("Köpunderlag").Range("B2"), Sheets("Köpunderlag").Range("B2").End(xlDown))
Set rngArray2 = Range(Sheets("Köpunderlag").Range("G2"), Sheets("Köpunderlag").Range("G2").End(xlDown))

KMyarray = Application.Transpose(Range(rngArray1, rngArray2))


 StrLookForString = Sheets("OFIC Köp").Cells(i, 5) & Sheets("OFIC Köp").Cells(i, 12)
        BoolHit = FindLoop(KMyarray, StrLookForString)

If BoolHit = True Then
SomeCodeHere
Else
Something else


And this is the function which is not working - my guess:


VBA Code:
Function FindLoop(arr, val) As Boolean
' Function to find matching value in array.
    Dim r As Long
    For r = 1 To UBound(arr, 1)
        If CStr(arr(r, 1)) & CStr(arr(r, 2)) = val Then
            FindLoop = True
            arr(r, 1) = "OK"
            arr(r, 2) = "OK"
            Exit Function
        End If
    Next r
End Function

This is what is not working (my guess): If CStr(arr(r, 1)) & CStr(arr(r, 2)) = val Then


Would really appreciate if anyone can help me, I am stuck here.
 
How come it is getting from C D E F & G? It should be from B2 and then going down as far as there is data in B (LastRow)?
Same with G2 to LastRow. Which part of the code includes D E F? It is not suppose to do so, so I might fix that..
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This part of your code is doing it
VBA Code:
kmyarray = Application.Transpose(Range(rngArray1, rngArray2))
Will both B & G have the same number of rows?
 
Upvote 0
Hmm okey then that is my only issue atm.

What should I write if I only want

B2 -> Lastrow
G2 -> Lastrow


VBA Code:
    Set rngArray1 = Range(Sheets("Köpunderlag").Range("B2"), Sheets("Köpunderlag").Range("B2").End(xlDown))
    Set rngArray2 = Range(Sheets("Köpunderlag").Range("G2"), Sheets("Köpunderlag").Range("G2").End(xlDown))

 
   KMyarray = Application.Transpose(Range(rngArray1, rngArray2))


And yes, they should do have the same number of rows.
 
Upvote 0
I would tend to keep them as two arrays & use it like
VBA Code:
With Sheets("Köpunderlag")
   rngArray1 = .Range("B2", .Range("B2").End(xlDown)).Value2
   rngArray2 = .Range("G2", .Range("G2").End(xlDown)).Value2
End With

'start loop here
StrLookForString = Sheets("OFIC Köp").Cells(i, 5) & Sheets("OFIC Köp").Cells(i, 12)
        BoolHit = FindLoop(rngArray1, rngArray2, StrLookForString)

If BoolHit = True Then
'SomeCodeHere
Else
'Something else
End If

End Sub


Function FindLoop(arr1, arr2, val) As Boolean
' Function to find matching value in array.
    Dim r As Long
    For r = 1 To UBound(arr1, 1)
        If CStr(arr1(r, 1)) & CStr(arr2(r, 1)) = val Then
            FindLoop = True
            arr1(r, 1) = "OK"
            arr2(r, 1) = "OK"
            Exit Function
        End If
    Next r
End Function
 
Upvote 0
rngArray1 = Sheets("Köpunderlag").Range("O1", Sheets("Köpunderlag").Range("O1").End(xlDown)).Value2
rngArray2 = Sheets("Köpunderlag").Range("S1", Sheets("Köpunderlag").Range("S1").End(xlDown)).Value2

working :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
May I ask one last question in the same matter?

VBA Code:
Function FindLoop(arr1, arr2, val) As Boolean
' Function to find matching value in array.
    Dim r As Long
    For r = 1 To UBound(arr1, 1)
        If CStr(arr1(r)) & CStr(arr2(r)) = val Then
            FindLoop = True
            arr1(r) = "OK"
            arr2(r) = "OK"
            Exit Function
        End If
    Next r
End Function

This is my Function now and it is working perfectly.

However, I want to be able to return the row (r) aswell on my

VBA Code:
If BoolHit = True Then
'SomeCodeHere
Else
'Something else
End If

At the moment I am writing
VBA Code:
            Sheets("Dualitet").Cells(i + 3, 11).Value = srngArray1(i)
            Sheets("Dualitet").Cells(i + 3, 12).Value = srngArray2(i)


But the "i" here (on srngArray1(i) and not in the Cells) is with a different row because I am looping on something else. I want that when the "FindLoop = True" the r it finds it will be the "i" on my code above. How do I do this? Return the row where it finds the True Value.
 
Upvote 0
How about
VBA Code:
Function FindLoop(arr1, arr2, val) As Long
' Function to find matching value in array.
    Dim r As Long
    For r = 1 To UBound(arr1, 1)
        If CStr(arr1(r)) & CStr(arr2(r)) = val Then
            FindLoop = r
            arr1(r) = "OK"
            arr2(r) = "OK"
            Exit Function
        End If
    Next r
End Function
and then change BoolHit to long & use
VBA Code:
If BoolHit >0 Then
 
Upvote 0
Will try. Am I suppose to put this like
VBA Code:
Sheets("Dualitet").Cells(i + 3, 12).Value = srngArray2(FindLoop)
 
Upvote 0
No, it needs to be BooHit, not Findloop
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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