我正在尝试在Excel 2007中创建用户定义函数。该函数位于VBA模块内,并接受单个参数,即Range对象。问题是接收时的参数始终为EMPTY。我错过了什么?
Function ConcatenateCells(p_objConcatArea As Range) As String
'******************************************
' Initialize Variables
'******************************************
Dim strReturnString As String
'******************************************
' Process Each Cell In Range
'******************************************
strReturnString = ""
For Each objCellValue In p_objConcateArea
If (objCellValue <> "") Then
If (strReturnString <> "") Then strReturnString = strReturnString & ", "
strReturnString = strReturnString & objCellValue
End If
Next
'******************************************
' Return Final Value
'******************************************
ConcatenateCells = strReturnString
End Function
在Excel工作表中,我在单元格内写了以下公式:
=ConcatenateCells(B22:B25)
我的结果是 #VALUE!。我逐步完成了代码,发现参数(p_objConcateArea)始终是EMPTY。为什么我不能将范围传递给我的UDF?
帮助!
-Ben
答案 0 :(得分:3)
对于未使用Option Explicit时遇到的问题,这是一个“经典”案例。我纠正了你的错字
Option Explicit
Function ConcatenateCells(p_objConcatArea As Range) As String
'******************************************
' Initialize Variables
'******************************************
Dim strReturnString As String
Dim objCellValue
'Dim p_objConcateArea
'******************************************
' Process Each Cell In Range
'******************************************
strReturnString = ""
For Each objCellValue In p_objConcatArea
If (objCellValue <> "") Then
If (strReturnString <> "") Then strReturnString = strReturnString & ", "
strReturnString = strReturnString & objCellValue
End If
Next
'******************************************
' Return Final Value
'******************************************
ConcatenateCells = strReturnString
End Function