arrays - 数组 vlookup 的运行时错误类型不匹配
问题描述
我有遇到
运行时错误 13 类型不匹配
在以下代码运行时。基本上我想使用第 31 列中的值在另一张表中查找以返回第 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
解决方案
我相信以下内容会按您的预期工作,跳过空白单元格,您可以这样做:
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
推荐阅读
- azure - Kubernetes(在 AKS 中)中的 ImagePullBackOff 错误,同时从 ACR 中提取图像并且所有 pod 都被驱逐
- c# - 在 SpecFlow 上使用 xunit.gherkin.quick 时,如何处理转换以便实现
? - android - 在 iOS 中,相当于 Android 中的 android.widget.Scroller
- c# - 如何检测文件缓冲区是否已被刷新
- php - 在通过控制器的某些视图中包含不同的变量
- java - Spark中DataFrame的正确连接?
- java - UpdateQuery 不执行。如何修复此代码?
- html - 如何在 htaccess 中从 site.com/home 路由到 site.com/home.html(删除 .html)?
- reactjs - React js,css url路径显示生产中的应用程序
- java - 如何从 Firebase 实时数据库获取数据到 ListView?