我遇到了
运行时错误13类型不匹配
在以下代码下运行。基本上,我想使用第31列中的值在另一张工作表中进行vlookup,以返回第32列中的搜索值,返回第33列中的搜索日期。
请帮助我。
Sub vlookupFU()
'vlookupfollowup material & Eff-out date
Dim wkbNPI As Workbook
Dim wksPT As Worksheet
Dim wksFU As Worksheet
Set wkbNPI = ActiveWorkbook
Set wksPT = wkbNPI.Sheets("Packaging tracking")
Set wksFU = wkbNPI.Sheets("FollowUpMaterial")
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Dim lrw2 As Long
lrw2 = wksPT.Cells(Rows.Count, "A").End(xlUp).row
Dim PTarray As Variant
Dim i As Long, j As Long
PTarray = wksPT.Range("A7:AG" & lrw2)
Dim Oldcode As String
Dim FUM As String 'Follow up material code
Dim FUMD As String 'Follow up material date
For i = 1 To lrw2
Oldcode = PTarray(i, 31)
If Oldcode <> 0 Then 'where I have error type mismatch
FUM = wf.vlookup(PTarray(i, 31), wksFU.Range("B:R"), 13, False) 'vlookup follow up material
FUMD = wf.vlookup(PTarray(i, 31), wksFU.Range("B:R"), 17, False) 'vlookup follow up material effective out date
FUM = PTarray(i, 32)
FUMD = PTarray(i, 33)
End If
Next i
End Sub
答案 0 :(得分:0)
我相信以下内容将按您期望的方式工作,跳过空白单元格,您可以这样做:
If Oldcode <> "" Then
请参见下面的修改后代码,我也删除了lrw2
,因为它未在代码中使用,需要考虑的另一点是,不要将Vlookup返回的值放在除变量,这样就不会将值添加到您的Excel工作表中吗?不确定这是否是您正在从事的工作...:
Sub vlookupFU()
'vlookupfollowup material & Eff-out date
Dim wkbNPI As Workbook: Set wkbNPI = ThisWorkbook
Dim wksPT As Worksheet: Set wksPT = wkbNPI.Sheets("Packaging tracking")
Dim wksFU As Worksheet: Set wksFU = wkbNPI.Sheets("FollowUpMaterial")
Dim wf As WorksheetFunction: Set wf = Application.WorksheetFunction
Dim lrw2 As Long, i As Long ', j As Long
Dim PTarray As Variant
Dim Oldcode As String, FUM As String, FUMD As String
'lrw2 = wksPT.Cells(Rows.Count, "A").End(xlUp).Row 'not used so no need for this line
PTarray = wksPT.Range("A7:AG" & lrw2)
For i = 1 To wksPT.Range("A7:AG" & lrw2).Rows.Count
Oldcode = PTarray(i, 31)
If Oldcode <> "" Then
FUM = wf.VLookup(PTarray(i, 31), wksFU.Range("B:R"), 13, False) 'vlookup follow up material
FUMD = wf.VLookup(PTarray(i, 31), wksFU.Range("B:R"), 17, False) 'vlookup follow up material effective out date
'FUM = PTarray(i, 32) 'you get the value above and then you overwrite it here??
'FUMD = PTarray(i, 33) 'you get the value above and then you overwrite it here??
End If
Next i
End Sub