What I do wrong???

crazyboy037

New Member
Joined
May 20, 2015
Messages
2
Hi!

I'm from Spain and I'm tryng to emulate the buscarv function with VBA. I'm using the Vlookup property but it doesn't work. Appears the 2042 error. What I do wrong? I supose that could be something relationed about the format of te cells... but I'm not sure...


Code:
Dim Fecha As Date
Dim Disponible As Integer
Dim Disp As Variant
Value As Variant
Rango As Range

Sub PrevDisp_FMT_VLC()

Fecha = Worksheets("PREVISIONES Y PEDIDOS").Range("U29").value
Value = Fecha
Set Rango = worksheets("RESUMEN").Range("A6:BM:1000)

Disp=Application.Vlookup(value, Rango, 7, False)

If IsError(Disp) Then
Disponible =0
Else
Disponible = Disp
End If

Worksheets("PREVISIONES Y PEDIDOS"). Range("I32"). value = Disponible

End Sub


The macro retunrs into the Cell I32 a 0 and it's wrong... It should be other number...
 
Last edited by a moderator:
You are right about double/long variable. I try with long and it also gives me the same error (as I was expecting).

It is really very strange.

Thank you anyway.


Hi,

It is worth adding a .Value so that VBA knows exactly what it is supposed to be doing:

Code:
If Cells(rowQCM, 3).Value = MaterialTag Then

Also, this should not be a problem, but I usually make row numbers Long as in:

Code:
Dim rowQCM as Long

They will never need to be fractional and they need to work with up to a million rows.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What is the value of cells(rrowQCM,3) when you get the error?

In the immediate pane with the code in debug what does this return?

? cells(rrowQCM,3).value
 
Upvote 0
What is the value of cells(rrowQCM,3) when you get the error?

I need help to solve an Error2042 error in the code line below in bold.

Sound like you have #N/A errors in the range; try changing the section of the code below to:

Rich (BB code):
   For rowMaterial = 2 To 50000
        FirstITRFound = True
        NumberOfQCFs = 0
        MaterialTag = Cells(rowMaterial, 1)
      ' Search for the Tag in the QCF sheet
        Sheets("QCM").Select
        For rowQCM = 2 To 50000
           If Not IsError(Cells(rowQCM, 3).Value) Then
                If Cells(rowQCM, 3) = MaterialTag Then
                    NumberOfQCFs = NumberOfQCFs + 1
                    QCFType = Cells(rowQCM, 18)
                        If FirstITRFound = True Then
                        FirstITRFound = False
                        Sheets("Material").Select
                        Cells(rowMaterial, 2) = QCFType
                            ElseIf FirstITRFound = False Then
                        MsgBox "FirstITRFound = :  " & FirstITRFound
                        Sheets("Material").Select
                        Cells(rowMaterial + NumberOfQCFs, 2).Select
                        Selection.EntireRow.Insert
                        ActiveCell.Value = QCFType
                    End If
                   'Sheets("Material").Select
                   MsgBox "QCFType found is:  " & QCFType
                   MsgBox "Number of QCFs found is:  " & NumberOfQCFs
                 End If
            End If
        Next
        MsgBox "Next Tag is:  " & Cells(rowMaterial + 1, 1)
    Next
 
Upvote 0
That;s it. Thank you.
Besides, I was not selecting the right sheet (the selection was out of the For cycle instead of inside), so it was testing a sheet with #N/A cells.

Thanks all!

Sound like you have #N/A errors in the range; try changing the section of the code below to:

Rich (BB code):
   For rowMaterial = 2 To 50000
        FirstITRFound = True
        NumberOfQCFs = 0
        MaterialTag = Cells(rowMaterial, 1)
      ' Search for the Tag in the QCF sheet
        Sheets("QCM").Select
        For rowQCM = 2 To 50000
           If Not IsError(Cells(rowQCM, 3).Value) Then
                If Cells(rowQCM, 3) = MaterialTag Then
                    NumberOfQCFs = NumberOfQCFs + 1
                    QCFType = Cells(rowQCM, 18)
                        If FirstITRFound = True Then
                        FirstITRFound = False
                        Sheets("Material").Select
                        Cells(rowMaterial, 2) = QCFType
                            ElseIf FirstITRFound = False Then
                        MsgBox "FirstITRFound = :  " & FirstITRFound
                        Sheets("Material").Select
                        Cells(rowMaterial + NumberOfQCFs, 2).Select
                        Selection.EntireRow.Insert
                        ActiveCell.Value = QCFType
                    End If
                   'Sheets("Material").Select
                   MsgBox "QCFType found is:  " & QCFType
                   MsgBox "Number of QCFs found is:  " & NumberOfQCFs
                 End If
            End If
        Next
        MsgBox "Next Tag is:  " & Cells(rowMaterial + 1, 1)
    Next
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,807
Members
449,468
Latest member
AGreen17

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