VBA not finding correct value.

lowej

New Member
Joined
Jun 30, 2018
Messages
8
Hello,

i am working on a VBA that should find a match in the table column A of sheet 1 and match it in a table on sheet 2 and select column 6 cell value.
but when i run the code it selects the wrong row and value. what i'm i doing wrong.
Code:
Sub test()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim oCell As Range
    Dim myArray() As Variant
    i = 2
    
    Set ws1 = ThisWorkbook.Sheets("Voeding")
    Set ws2 = ThisWorkbook.Sheets("Waarde")
    
    Do While ws1.Cells(i, 1).Value <> ""
        Set oCell = ws2.Range("A:A").Find(What:=ws1.Cells(i, 1), LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows)
        If Not oCell Is Nothing Then ws2.Cells(i, 6) = oCell.Offset(0, 1)
        If IsNumeric(ws2.Cells(i, 6).Value) Then
            ReDim Preserve myArray(x)
             myArray(x) = Cells(i, 6).Value
             myArray(x) = myArray(x) + 1
        End If
        i = i + 1
    Loop

    'Print values to Immediate Window (Ctrl + G to view)
        For x = LBound(myArray) To UBound(myArray)
            Debug.Print myArray(x)
        Next x
    Set ws1 = Nothing
    Set ws2 = Nothing
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Don't know if this is part of your problem, but this line needs a worksheet qualifier:

myArray(x) = ws2.Cells(i, 6).Value
 
Upvote 0
Thanks for the reply. It fix an issue that is didn't know i had yet hahah But is doesn't fix the problem.
I don't know if the loop that i have is correct...?
 
Upvote 0

Forum statistics

Threads
1,215,791
Messages
6,126,930
Members
449,349
Latest member
Omer Lutfu Neziroglu

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