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:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi and welcome to the MrExcel Message Board,

I think you have a typo in your code because it won't compile like that.
However, I suspect the main problem is that you are looking up dates. In which case you need to put a CLng() round your value, For example:

Code:
Set Rango = worksheets("RESUMEN").Range("A6:BM1000")
Disp=Application.Vlookup(CLng(value), Rango, 7, False)
 
Upvote 0
Hi all,

I need help to solve an Error2042 error in the code line below in bold.
I think it should be easy but I can't find the problem. Probably I am just using the wrong type of variable.

The message that appears in my screen is:
Run-time error '13'
Type mismatch.

Thanks in advance.

________________________________
Rich (BB code):
Sub A_QCF_TYPE()
'
'
'    Sheets("Material").Select
'    Sheets("QCM").Select


   Dim FirstITRFound As Boolean
   Dim MaterialTag As String
   Dim QCFType As String
   Dim NumberOfQCFs As Integer
   Dim CheckingTagFromQCM As String
      
   ' workbook must have the material sheet from takeover as the first sheet and QCM sheet from takeover as the 2nd sheet
   ' these two sheets will be renamed to "Material" and "QCM"
   Sheets(1).Select
   Sheets(ActiveSheet.Name).Name = "to be renamed 1"
   Sheets(ActiveSheet.Name).Name = "Material"
   
   Sheets(2).Select
   Sheets(ActiveSheet.Name).Name = "to be renamed 2"
   Sheets(ActiveSheet.Name).Name = "QCM"
   
   ' Creates a new column to put the QCF Types
   Sheets("Material").Select
   Range("B2").Select
   Selection.EntireColumn.Insert
   
   ' Searches for the several QCF Types for each Tag of Material
   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 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
              
        Next
        
        MsgBox "Next Tag is:  " & Cells(rowMaterial + 1, 1)
    Next
End Sub
 
Last edited by a moderator:
Upvote 0
Don't have Excel at the moment, but you haven't dimmed "rowQCM" as Long
 
Upvote 0
Don't have Excel at the moment, but you haven't dimmed "rowQCM" as Long

Sorry.
I forgot to mention the public variable declaration:

Public rowMaterial As Double
Public rowQCM As Double

(rest of the code as written above )

Thanks.
 
Upvote 0
Runs Ok for me....if you run it manually, where is the error line ??
 
Upvote 0
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

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

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