大佬教程收集整理的这篇文章主要介绍了在 VBA Excel 中使用索引匹配函数进行错误处理,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 VBA 很陌生,我已经实现了一个正确返回数据的索引/匹配函数。 唯一需要更正的是这段代码如何处理不匹配的值。在这种情况下,没有匹配的值会返回 #N/A 错误,而我只想返回零。
Sub Indexmatch()
Dim desTinationWs As Worksheet,dataWs As Worksheet
Dim desTinationLastRow As Long,dataLastRow As Long,x As Long
Dim IndexRng As Range,IndexRng2 As Range,MatchRng As Range
Set desTinationWs = ThisWorkbook.Worksheets("DesTination")
Set dataWs = ThisWorkbook.Worksheets("Population")
desTinationLastRow = desTinationWs.Range("A" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row
Set IndexRng = dataWs.Range("B2:B" & dataLastRow)
Set MatchRng = dataWs.Range("A2:A" & dataLastRow)
For x = 2 To desTinationLastRow
On Error Resume Next
a = Application.Match(desTinationWs.Range("A" & X).Value,MatchRng,0)
If IsError(a) Then
b = 0
Else
b = Application.Index(IndexRng,a)
End If
'desTinationWs.Range("C" & X).Value = Application.Index( _
'IndexRng,Application.Match(desTinationWs.Range("A" & X).Value,0))
On Error GoTo 0
Next x
End Sub
我如何在上面的代码中翻译它? 提前致谢
使用应用程序版本匹配,就像这样其中 b 将是您的目标范围。
{{1}},
完整代码如下:
Sub IndexMatch()
Dim desTinationWs As Worksheet,dataWs As Worksheet
Dim desTinationLastRow As Long,dataLastRow As Long,x As Long
Dim IndexRng As Range,IndexRng2 As Range,MatchRng As Range
Set desTinationWs = ThisWorkbook.Worksheets("DesTination")
Set dataWs = ThisWorkbook.Worksheets("Population")
desTinationLastRow = desTinationWs.Range("A" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row
Set IndexRng = dataWs.Range("B2:B" & dataLastRow)
Set MatchRng = dataWs.Range("A2:A" & dataLastRow)
For x = 2 To desTinationLastRow
On Error Resume Next
a = Application.Match(desTinationWs.Range("A" & X).Value,MatchRng,0)
If IsError(a) Then
b = 0
Else
b = Application.Index(IndexRng,a)
End If
'desTinationWs.Range("C" & X).Value = Application.Index( _
'IndexRng,Application.Match(desTinationWs.Range("A" & X).Value,0))
On Error GoTo 0
Next x
End Sub
,
试试我的变体
@H_723_5@my_algorithm{}(std::string_view{s});
关于IFNA函数,可以阅读here
以上是大佬教程为你收集整理的在 VBA Excel 中使用索引匹配函数进行错误处理全部内容,希望文章能够帮你解决在 VBA Excel 中使用索引匹配函数进行错误处理所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。