Excel 如何使用VBA从宏结果/以及多个查找中删除“N/A”?
我一直在试图整理一份电子表格,用我从不同地方得到的一些宏来追踪一些活动,因为我自己写不出来 我有两个问题:Excel 如何使用VBA从宏结果/以及多个查找中删除“N/A”?,excel,vba,vlookup,Excel,Vba,Vlookup,我一直在试图整理一份电子表格,用我从不同地方得到的一些宏来追踪一些活动,因为我自己写不出来 我有两个问题: 此宏从一个工作表执行查找,并在另一个工作表中重新输入值 Dim lngLastRow As Long Dim wsOutput As Worksheet Dim wsSource As Worksheet Application.ScreenUpdating = False Set wsOutput = Sheets("sheet 1")
Dim lngLastRow As Long
Dim wsOutput As Worksheet
Dim wsSource As Worksheet
Application.ScreenUpdating = False
Set wsOutput = Sheets("sheet 1") 'Sheet name for the following VBA to fill in
Set wsSource = Sheets("sheet 2") 'Sheet name containing completed data for VLOOKUP
lngLastRow = wsOutput.Range("A:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With wsOutput
With .Range("B2:B" & lngLastRow)
.Formula = "=VLOOKUP(C2,'" & CStr(wsSource.Name) & "'!A:G,6,FALSE)"
.Value = .Value 'Convert above formula to a value. Comment out or remove if you want the formula to remain
End With
End With
Set wsOutput = Nothing
Set wsSource = Nothing
Application.ScreenUpdating = True
End Sub
所以我从第1页的C列在第2页的表格中查找。
宏进行查找并返回表1中B列中的值。
宏对表1中C列中的所有值执行此操作
问题:如果没有找到值,它将返回N/A,我如何消除它,我不知道如何在VBA中使用iferror.Formula=“=IF(ISNA(VLOOKUP(C2,”&CStr(wsSource.Name)&“!A:G,6,FALSE)),”,VLOOKUP(C2,”&CStr(wsSource.Name)&“!A:G,6,FALSE)),“
.Formula=“=VLOOKUP(A2),&CStr(wsSource.Name)和“!C:W,3,FALSE)”
对于第一个公式,我仍然没有答案。但对于第二个问题:
你说你想在下一个公式中使用D30:D单元格。所以我想你应该使用A30中的值。那么我认为应该改变以下几点:
.Range(“D2:D30”和lngLastRow)
.Formula=“=VLOOKUP(A2,”&CStr(wsSource.Name)&“!C:W,4,FALSE)”
进入
.Range(“D30:D”和lngLastRow)
.Formula=“=VLOOKUP(A30,”&CStr(wsSource.Name)&“!C:W,4,FALSE)”
Checkout
IsError(.Value)
如果单元格中的值为错误,则返回True
。因此,如果有错误,您可以使用.Value=”“
,如果没有错误,则转换.Value=.Value
请同时提供一些示例数据或屏幕截图。这里的任何人都不太可能从可疑来源下载任何东西请注意,您可以使用F8
逐步检查代码,并在每个步骤中检查变量的值。这样你就可以发现哪里出了问题。弄清楚问题到底是什么,并精确地回答你的问题(太宽了,无法给出正确的答案)。谢谢你的回答,我会确保这样做,我不知道f8这是非常有用的,我试过了,我没有真正得到错误,只是我的宏没有按我想要的方式工作,我已经用更多信息更新了帖子谢谢Gert,ISNA有效,我已经在原始帖子中添加了第二个宏,或者您可以使用IFERROR
:“=IFERROR(VLOOKUP(A2),”&CStr(wsSource.Name)和“!C:W,3,FALSE),”)“
我尝试过的宏中没有单引号。公式=“=VLOOKUP(A2,&CStr(wsSource.Name)&”!C:W,3,FALSE)”没有引号,就没有运气了
Dim lngLastRow As Long
Dim wsOutput As Worksheet
Dim wsSource As Worksheet
Application.ScreenUpdating = False
Set wsOutput = Sheets("New tickets") 'Sheet name for the following VBA to fill in
Set wsSource = Sheets("lookup") 'Sheet name containing completed data for VLOOKUP
lngLastRow = wsOutput.Range("C:W").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With wsOutput
'Formula for School Name
With .Range("C2:C" & lngLastRow)
.Formula = "=VLOOKUP(A2,'" & CStr(wsSource.Name) & "'!C:W,3,FALSE)"
.Value = .Value 'Convert above formula to a value. Comment out or remove if you want the formula to remain
End With
End With
With wsOutput
'Formula for School Name
With .Range("D2:D30" & lngLastRow)
.Formula = "=VLOOKUP(A2,'" & CStr(wsSource.Name) & "'!C:W,4,FALSE)"
.Value = .Value 'Convert above formula to a value. Comment out or remove if you want the formula to remain
End With
End With
Set wsOutput = Nothing
Set wsSource = Nothing
Application.ScreenUpdating = True