[英]excel macro 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 as Single的声明不正确。 将变量声明为Variant应该可以解决该错误。
子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
结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.