Excel 在VBA函数上处理IfError
我需要在VBA中创建一个公式,其中需要有iferror选项 基本上,我在Excel中的公式是:Excel 在VBA函数上处理IfError,excel,vba,function,vlookup,Excel,Vba,Function,Vlookup,我需要在VBA中创建一个公式,其中需要有iferror选项 基本上,我在Excel中的公式是: Iferror(vlookup(A1&A2;A:B;2;FALSE);vlookup(A1;P:Q;2;FALSE)) 然后我尝试在VBA中只复制一个公式 Function DestAcc ( Account as string, FA as string) Dim rng1,rng2 as range With Workbooks(“ACCOUNTS”).worksheets(“Ac
Iferror(vlookup(A1&A2;A:B;2;FALSE);vlookup(A1;P:Q;2;FALSE))
然后我尝试在VBA中只复制一个公式
Function DestAcc ( Account as string, FA as string)
Dim rng1,rng2 as range
With Workbooks(“ACCOUNTS”).worksheets(“Accounts”)
Set rng1=.Range(.cells(1,1),cells(50000,2)
Set rng2=.Range(.cells(1,16),cells(50000,17)
DestAcc=WorksheetFunction.IfError(WorksheetFunction.VLookup(Account & FA, rng1, 2, False), WorksheetFunction.VLookup(Account, rng2, 2, False))
End function
然后我在工作簿中放了一个空单元格
=DestAcc(C1;D1)
我得到的结果是#值
如果我分别尝试这两个成员,我会得到一个结果,如果我尝试带有“IfError”的函数,我总会得到#值
有人能帮我吗
谢谢试试看
不要使用工作表功能
使用应用程序
更新:设置对具有扩展名的工作簿的引用
Function DestAcc(Account As String, FA As String)
Dim accountsWorkbook As Workbook
Dim accountsWorksheet As Worksheet
Dim accountsWithFARange As Range
Dim AccountsOnlyRange As Range
Dim resultAccountFA As Variant
Dim resultAccount As Variant
Set accountsWorkbook = Workbooks("Accounts.xlsm")
Set accountsWorksheet = accountsWorkbook.Worksheets("Accounts")
With accountsWorksheet
Set accountsWithFARange = .Range("$A$1:$B$50000") ' $A$1:$B$50000
Set AccountsOnlyRange = .Range("$P$1:$Q$50000") ' $P$1:$Q$50000
End With
resultAccountFA = Application.VLookup(Account & FA, accountsWithFARange, 2, False)
resultAccount = Application.VLookup(Account, AccountsOnlyRange, 2, False)
DestAcc = IIf(Not IsError(resultAccountFA), resultAccountFA, resultAccount)
End Function
注:
您的代码有几个缺陷:
- 您正在将rng1定义为变量(此尺寸rng1、rng2作为范围与尺寸rng1作为范围、rng2作为范围不同)
- 您没有关闭With块(缺少End With)
- 尝试将变量命名为任何人都能理解的名称(rng1没有多大意义)
- 尝试写短行(与worksheetfunction.vlookup混合的iferror可以拆分为两行)
- 设置范围的方式很难理解。您可以使用Set rng1=.Range($A$1:$B$50000)
- 将所有三个程序放在 包含此代码的工作簿
- 调整
、luAccount
和luFA
注意:您必须使用打开工作簿的扩展名(ws
).xlsm、.xlsx、.xls
- 此解决方案将忽略情况,即
A=A
- 您已经知道如何在
中使用它Excel
Option Explicit
' A Conditional Consecutive Lookup
Function DestAcc(Account As String, FA As String)
Application.Volatile
Dim luAccount As Variant, luFA As Variant
' Specify: First Rows, Match Columns, Value Columns
luAccount = Array(1, 1, 2)
luFA = Array(1, 16, 17)
Dim ws As Worksheet
' Either on the ActiveSheet:
'Set ws = Cells.Worksheet ' or Application.ThisCell.Worksheet
' or on a specified worksheet:
On Error GoTo exitProcedure
Set ws = Workbooks("Accounts.xlsm").Worksheets("Accounts")
On Error GoTo 0
Dim rng As Range
Dim vMatch As Variant, vValue As Variant
Dim MatchIndex As Long
Dim Criteria As String
' 1st LookUp
Set rng = getPartialColumn(ws, luAccount(0), luAccount(1))
If rng Is Nothing Then GoTo SecondLookUp
vMatch = rng: vValue = rng.Offset(, luAccount(2) - luAccount(1))
Criteria = Account & FA: GoSub findMatch
' 2nd LookUp
SecondLookUp:
Set rng = getPartialColumn(ws, luFA(0), luFA(1))
If rng Is Nothing Then GoTo exitProcedure
vMatch = rng: vValue = rng.Offset(, luFA(2) - luFA(1))
Criteria = Account: GoSub findMatch
GoTo exitProcedure
findMatch:
MatchIndex = getMatchIndex(Criteria, vMatch)
If MatchIndex > 0 Then GoTo returnLookup
Return
returnLookup:
DestAcc = vValue(MatchIndex, 1)
GoTo exitProcedure
exitProcedure:
End Function
' Returns the column range from a specified row to the last non-empty row.
Function getPartialColumn(WorksheetObject As Worksheet, _
Optional ByVal FirstRowNumber As Long = 1, _
Optional ByVal columnNumber As Long = 1) As Range
Dim rng As Range
With WorksheetObject
Set rng = .Columns(columnNumber).Find(What:="*", _
LookIn:=xlFormulas, SearchDirection:=xlPrevious)
If rng Is Nothing Then Exit Function
Set getPartialColumn = .Range(.Cells(FirstRowNumber, columnNumber), rng)
End With
End Function
' Returns the index of a found value in an array, or 0 if not found.
Function getMatchIndex(MatchValue As Variant, MatchArray As Variant) As Long
If Not IsError(Application.Match(MatchValue, MatchArray, 0)) Then
getMatchIndex = Application.Match(MatchValue, MatchArray, 0)
End If
End Function
请回答您的问题,并张贴完整的代码,您如何称呼它,以及每种情况下的结果。谢谢您的回答Ricardo,我的全部代码都在帖子中。我只是试着让每个成员在不同的功能中独立工作,他们都能工作,但一起工作就不行了。您能发现错误吗?
WorksheetFunction.XYZ
是早期绑定的,会引发VBA运行时错误,您需要使用On error
语句来处理这些错误Application.XYZ
工作表函数是后期绑定的,返回一个Variant/Error
:使用IsError
函数惯用地验证Variant
是否包含Error
类型的值。感谢您的帮助Ricardo。不幸的是,它仍然给我相同的结果#值。这可能是问题所在工作簿(“帐户”)。工作表(“帐户”)
是否从其他工作簿调用该函数?它是否位于会计表所在的同一工作簿中?不,它是另一个打开的工作簿。我认为问题不在这里,因为如果我不使用IIF,只选择一个Vlookup函数,函数就会工作。。。问题是当我有两种选择。。。#值不能被视为错误吗?如果它有效,请记住标记答案,以便其他人在上次更正时可以找到它。现在我得到一个零(不是#值错误)