excel宏vba vlookup

时间:2015-09-18 21:53:09

标签: vba excel-vba vlookup excel

这是我第一次在VBA中使用vlookup而我收到错误类型不匹配的行:vLook = application.worksheet ...

如果有人能澄清为什么会这样,我会很感激。非常感谢你。

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

2 个答案:

答案 0 :(得分:0)

错误消息表明名为vLook的变量声明为Single是不正确的。 将变量声明为Variant应该可以修复错误。

答案 1 :(得分:0)

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

结束子