用户定义的函数不适用于其他工作表
User-defined function is not working for other sheets
enter image description here如何解决这种情况?
如您所知,EXCEL 中的公式 VLOOKUP 显示在 table 中找到的第一个值。有时,您需要从 table 中获取所有值作为列表。
因此,我想到应该有这样一个函数(公式):
=VLOOKUP_NUMBER_VALUE(DesiredValue, ColumnWhereWeSearch, ColumnFromWhereWeWantToGet,Number)
代码:
Function VLOOKUP_NUMBER_VALUE(DesiredValue As Variant, ColumnWhereWeSearch As Range, ColumnFromWhereWeWantToGet As Range, Number As Integer)
VBA代码:
On Error GoTo VLOOKUP_NUMBER_VALUE_ERR
n = 0
'we start to loop column if there is our desired value
For Each cell In ColumnWhereWeSearch
'we don't want to check up empty cells
If cell <> "" Then
'if the cell has our desired value we number it using n variable
If cell = DesiredValue Then n = n + 1
'if this n equals our Number in function arguments we need to get row number in cell in order to find the value in the column where we want to get lookup
If n = Number And cell = DesiredValue Then RowNumber = cell.Row
End If
Next cell
'here we just get column number our third variable
ColumnNumberResult = ColumnFromWhereWeWantToGet.Column
'Result:
VLOOKUP_NUMBER_VALUE = Cells(RowNumber, ColumnNumberResult)
Exit Function
VLOOKUP_NUMBER_VALUE_ERR:
'this row gives "-" when error occurs
VLOOKUP_NUMBER_VALUE = "-"
End Function
问题是,当我在其他 sheet 书籍的参数范围内使用时,此功能不起作用。
而我大概明白了为什么。事实证明我没有指定文档的名称,参数中或函数中的 sheet,我需要以某种方式在函数中获取它。
中有一个隐含的ActiveSheet
VLOOKUP_NUMBER_VALUE = Cells(RowNumber, ColumnNumberResult)
要修复,请使用 ColumnWhereWeSearch.Parent
,这是问题范围所在的工作表:
VLOOKUP_NUMBER_VALUE = ColumnWhereWeSearch.Parent.Cells(RowNumber, ColumnNumberResult)
enter image description here如何解决这种情况?
如您所知,EXCEL 中的公式 VLOOKUP 显示在 table 中找到的第一个值。有时,您需要从 table 中获取所有值作为列表。 因此,我想到应该有这样一个函数(公式):
=VLOOKUP_NUMBER_VALUE(DesiredValue, ColumnWhereWeSearch, ColumnFromWhereWeWantToGet,Number)
代码:
Function VLOOKUP_NUMBER_VALUE(DesiredValue As Variant, ColumnWhereWeSearch As Range, ColumnFromWhereWeWantToGet As Range, Number As Integer)
VBA代码:
On Error GoTo VLOOKUP_NUMBER_VALUE_ERR
n = 0
'we start to loop column if there is our desired value
For Each cell In ColumnWhereWeSearch
'we don't want to check up empty cells
If cell <> "" Then
'if the cell has our desired value we number it using n variable
If cell = DesiredValue Then n = n + 1
'if this n equals our Number in function arguments we need to get row number in cell in order to find the value in the column where we want to get lookup
If n = Number And cell = DesiredValue Then RowNumber = cell.Row
End If
Next cell
'here we just get column number our third variable
ColumnNumberResult = ColumnFromWhereWeWantToGet.Column
'Result:
VLOOKUP_NUMBER_VALUE = Cells(RowNumber, ColumnNumberResult)
Exit Function
VLOOKUP_NUMBER_VALUE_ERR:
'this row gives "-" when error occurs
VLOOKUP_NUMBER_VALUE = "-"
End Function
问题是,当我在其他 sheet 书籍的参数范围内使用时,此功能不起作用。
而我大概明白了为什么。事实证明我没有指定文档的名称,参数中或函数中的 sheet,我需要以某种方式在函数中获取它。
ActiveSheet
VLOOKUP_NUMBER_VALUE = Cells(RowNumber, ColumnNumberResult)
要修复,请使用 ColumnWhereWeSearch.Parent
,这是问题范围所在的工作表:
VLOOKUP_NUMBER_VALUE = ColumnWhereWeSearch.Parent.Cells(RowNumber, ColumnNumberResult)