Error 2042 problem with vlookup

dfeteau

New Member
Joined
Jun 26, 2008
Messages
7
I'm having a problem trying to use a simple vlookup request and match it to
another variable.

Two tables on two sheets. The problem I'm having is that when the vlookup
request to the array comes back as an error when it shouldn't. I've looked
at this code 100 times and I can't find an error. Any help??

Sheet1
500 Description A
501 Description B
502 Description C
503 Description D

Sheet2
500 5/16/2011 TEST0
500 5/16/2011 TEST1
500 5/16/2011 TEST2
501 5/16/2011 TEST3
503 5/16/2011 TEST4
503 5/16/2011 TEST5
504 5/16/2011 TEST6

Code:
Sub KeepTrying()

Sheets("Sheet2").Activate
'Get  Count
Range("A1").Select
    i = 0
    Do Until ActiveCell.Value =  ""
        ActiveCell.Offset(1, 0).Select
        i = i + 1
     Loop

'Create Array using count
Dim tableA() As Variant
ReDim  tableA(1 To i, 1 To 3)
Range("A1").Select
x = 1
Do Until x = i +  1
    tableA(x, 1) = ActiveCell.Offset(0, 0).Value
    tableA(x, 2) = ActiveCell.Offset(0, 1).Value
    tableA(x, 2) = ActiveCell.Offset(0,  2).Value
    ActiveCell.Offset(1, 0).Select
    x = x +  1
Loop

'Check
Sheets("Sheet1").Activate
Range("A1").Select
     Do Until ActiveCell.Value = ""
        For d = 1 To i
        code =  ActiveCell.Value
        codeCheck = Application.VLookup(d, tableA, 1, False)  - PROBLEM!!!
        If code = codeCheck Then
             ActiveCell.Offset(0, 3).FormulaR1C1 = "Y"
            ActiveCell.Offset(1,  0).Select
        End If
        Next
    Loop

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I would suspect that one set of numbers is actually stored as text but you are looking up an actual number. In the lookup table, are the numeric codes left or right aligned by default? (ie without any manual alignment applied)
 
Upvote 0
Try something like this...

Code:
Sub KeepTrying()

    Dim cell As Range

    ' Check
    With Sheets("Sheet1")
        For Each cell In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            If Not Sheets("Sheet2").Range("A:A").Find( _
                                     What:=cell.Value, _
                                     LookIn:=xlValues, _
                                     Lookat:=xlWhole, _
                                     MatchCase:=False) Is Nothing Then
                cell.Offset(0, 3).Value = "Y"
            End If
        Next cell
    End With

End Sub
 
Upvote 0
In your original code, I suspect that VLookup is expecting a cell range where you have tableA and not an array variable.
 
Upvote 0
I would suspect that one set of numbers is actually stored as text but you are looking up an actual number. In the lookup table, are the numeric codes left or right aligned by default? (ie without any manual alignment applied)

I thought so too but I checked the Locals window and it shows the first dataArrray variable as a variant/double. The same with the variable i'm searching with.
 
Upvote 0
Try something like this...

Code:
Sub KeepTrying()

    Dim cell As Range

    ' Check
    With Sheets("Sheet1")
        For Each cell In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            If Not Sheets("Sheet2").Range("A:A").Find( _
                                     What:=cell.Value, _
                                     LookIn:=xlValues, _
                                     Lookat:=xlWhole, _
                                     MatchCase:=False) Is Nothing Then
                cell.Offset(0, 3).Value = "Y"
            End If
        Next cell
    End With

End Sub
This isn't bad but what I ultimately want to do is to add that variable to another array. Will doing it this way allow me to do that?
 
Upvote 0
Try something like this...

Code:
Sub KeepTrying()

    Dim cell As Range

    ' Check
    With Sheets("Sheet1")
        For Each cell In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            If Not Sheets("Sheet2").Range("A:A").Find( _
                                     What:=cell.Value, _
                                     LookIn:=xlValues, _
                                     Lookat:=xlWhole, _
                                     MatchCase:=False) Is Nothing Then
                cell.Offset(0, 3).Value = "Y"
            End If
        Next cell
    End With

End Sub
This worked fine. Thanks a lot!!
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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