简体   繁体   中英

excel macro vba vlookup

it's my first time using vlookup in VBA and I'm getting the error Type Mismatch for the line: vLook = application.worksheet...

If anyone can clarify why this is happening I'd appreciate it. Thank you very much.

Sub test3()

Dim text1 As String, text2 As String, text3 As String
Dim vLook As Single
Dim lookupRange As Range

Set lookupRange = Sheet3.Range("x5:y500")

Z = 5

Do While Sheet3.Cells(Z, 1) <> ""

text1 = Sheet3.Cells(Z, 23)

vLook = Application.WorksheetFunction.VLookup(text1, lookupRange, 2, False)

Z = Z + 1

Loop

End Sub

The error message is indicating that the declaration for your variable called vLook as Single was not correct. Declaring the variable as Variant instead should fix the error.

Sub vlookupJira()

On Error Resume Next
Dim Dept_Row As Long, Dept_Row1 As Long, Dept_Row2 As Long, Dept_Row3 As Long
Dim Dept_Clm As Long, Dept_Clm1 As Long, Dept_Clm2 As Long, Dept_Clm3 As Long
Dim LastRowRJ As Long, LastRowTE As Long, LastRowTJ As Long

LastRowRJ = Worksheets("Requirement_JIRAs").Cells(Worksheets("Requirement_JIRAs").Rows.Count, "A").End(xlUp).Row
LastRowTJ = Worksheets("Test_JIRAs").Cells(Worksheets("Test_JIRAs").Rows.Count, "A").End(xlUp).Row
LastRowTE = Worksheets("Test_Execution").Cells(Worksheets("Test_Execution").Rows.Count, "A").End(xlUp).Row

Table1 = Range("A2:A" & LastRowTE) ' Employee_ID Column from Employee table
Table2 = Worksheets("Requirement_JIRAs").Range("A2:C" & LastRowRJ) ' Range of Employee Table 1

Dept_Row = Range("B2").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm = Range("B2").Column

For Each cl In Table1
    Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.vlookup(cl, Table2, 3, False)
    Dept_Row = Dept_Row + 1
Next cl

Table3 = Range("C2:C" & LastRowTE) ' Employee_ID Column from Employee table
Table4 = Worksheets("Test_JIRAs").Range("A2:D" & LastRowTJ) ' Range of Employee Table 1

Dept_Row1 = Range("D2").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm1 = Range("D2").Column

Dept_Row2 = Range("E2").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm2 = Range("E2").Column

Dept_Row3 = Range("F2").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm3 = Range("F2").Column

For Each cl In Table3
    Cells(Dept_Row1, Dept_Clm1) = Application.WorksheetFunction.vlookup(cl, Table4, 2, False)
    Cells(Dept_Row2, Dept_Clm2) = Application.WorksheetFunction.vlookup(cl, Table4, 3, False)
    Cells(Dept_Row3, Dept_Clm3) = Application.WorksheetFunction.vlookup(cl, Table4, 4, False)
    Dept_Row1 = Dept_Row1 + 1
    Dept_Row2 = Dept_Row2 + 1
    Dept_Row3 = Dept_Row3 + 1
Next cl

With Worksheets("Test_Execution")
    .Rows(LastRowTE + 1 & ":" & .Rows.Count).Delete
End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM