VlookUp使用数字但不是文本

时间:2015-09-28 18:14:38

标签: excel-vba vlookup vba excel

我一直在使用VLookUp进行匹配numbers我希望尝试在text上使用exact匹配

当我这样做时,我什么也得不到,no匹配,即使存在匹配

Sub Match()

Dim I As Long, j As Long, cnt As Long
Dim lValue As Long
Dim Sheet1 As Worksheet, Sheet2 As Worksheet

Set Sheet1 = Worksheets("Sheet1")
Set Sheet2 = Worksheets("Sheet2")

I = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
j = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row

For cnt = 2 To j
On Error Resume Next
lValue = WorksheetFunction.VLookup(Sheet2.Range("A" & cnt), Sheet1.Range("A2:A" & I), 1, False)
If lValue <> 0 Then
    Sheet2.Range("C" & cnt).Interior.Color = RGB(255, 255, 0)
    Sheet2.Range("C" & cnt) = Sheet2.Range("A" & cnt).Value
End If
  lValue = 0
Next

End Sub

1 个答案:

答案 0 :(得分:1)

将lValue更改为变体

Dim lValue As variant