VBA Excel 自定义函数不能调用成功的问题的解决方法
问题以及解决方案的列举(持续更新中)
自定义函数是自动化各种Excel重复操作的重要函数,我们可以用它实现各种自己的奇葩需求。但是当我退出VBA代码编辑界面调用时,却发现Excel找不到我定义的函数,那么可能有以下很多种原因可以导致上述的问题:
- 在VBA中误写定义为Sub 子程序,而不是 Function 函数,导致Excel不能识别。
- 在当前Sheet或者工作簿下 写VBA,而不是创建一个新的模块编写VBA。(本人是这个错误)
- 在其他的工作簿下 写VBA,导致在当前excel文件下不识别。
- 函数名和Excel自带函数名重复导致冲突。
- 网传Office破解版不能调用自定义函数,但是我使用的就是破解版,也可以识别。因此仅做参考。
Function Merge_single(x As Range, y As Range) 'This function is to filter some infomations indicated and put them together
Dim wb As Object
Dim st As Object
Dim xx, yy As Range
Dim xstr, ystr As String
Dim rr As Integer
If x.rows.Count <> y.rows.Count Then
MsgBox "The variables you enter don't have the same column"
End If
Call del_text(x)
Call del_text(y)
ystr = Cells(y.Row, y.Column).Value
xstr = filter(x)
Merge_single = ystr & xstr
xstr = ""
ystr = ""
End Function
当解决问题后,在Excel界面可以像正常函数一样使用你的自定义函数。如下图所示。