excel宏vba vlookup

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel宏vba vlookup相关的知识,希望对你有一定的参考价值。

这是我第一次在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
答案

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

另一答案

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

结束子

以上是关于excel宏vba vlookup的主要内容,如果未能解决你的问题,请参考以下文章

使用特殊字符搜索单元格值时 VLOOKUP 的 Excel VBA 替代方案

Excel VBA自动填充只返回公式

带有范围的 Excel VBA Vlookup

excel 2010 vba。 sql查询列'like' vlookup

当关闭文档是excel vba为啥宏代码自动消失

Access中VBA中excel文件中的VLookup