Vba #名字?为数据链接创建自定义函数后出错
我正在链接历史数据源,这些数据源的地名拼写和排序不统一。我解决这个问题的方法是创建一个自定义Excel函数,该函数将删除名称中具有挑战性的部分(即标点符号“saint”等),将地名字符串中的所有字符转换为数值,将它们相加,同时保留第一个字母。这样,除了第一个字母外,字符出现的顺序应该是无关的。这是我的代码:Vba #名字?为数据链接创建自定义函数后出错,vba,excel,excel-formula,Vba,Excel,Excel Formula,我正在链接历史数据源,这些数据源的地名拼写和排序不统一。我解决这个问题的方法是创建一个自定义Excel函数,该函数将删除名称中具有挑战性的部分(即标点符号“saint”等),将地名字符串中的所有字符转换为数值,将它们相加,同时保留第一个字母。这样,除了第一个字母外,字符出现的顺序应该是无关的。这是我的代码: Option Explicit Public Function Scramble(ByVal str1 As String) ' This function is meant to
Option Explicit
Public Function Scramble(ByVal str1 As String)
' This function is meant to generate a unique index of all the characters used
' in the string, regardless of their order. It also removes ambiguous spellings
Dim count As Long, i As Long, firstLetter As String
' convert to upper case
str1 = UCase(str1)
' remove punctuation first
str1 = Replace(str1, ".", "")
str1 = Replace(str1, ",", "")
str1 = Replace(str1, "-", " ")
str1 = Replace(str1, "'", "")
' now ambiguous spellings
str1 = Replace(str1, "ST ", "")
str1 = Replace(str1, "SAINT ", "")
str1 = Replace(str1, "MAGNA", "GREAT")
str1 = Replace(str1, "PARVA", "LESS")
' now extract the first letter
firstLetter = Left(str1, 1)
' now prepositions
str1 = Replace(str1, "AT ", "")
str1 = Replace(str1, "IN ", "")
str1 = Replace(str1, "THE ", "")
str1 = Replace(str1, "UPON ", "ON")
' make sure to remove spaces last or else the searches will fail
str1 = Replace(str1, " ", "")
count = 0
For i = 1 To Len(str1)
count = count + Asc(Mid(str1, i, 1))
Next i
Scramble = firstLetter & Str(count)
End Function
我的问题是,当我在工作表中调用此函数时,它返回一个
#NAME?
错误,并且它似乎也导致我的CONCAT
工作表函数停止工作,同时调用#NAME?
错误。我有点不知所措,不知是什么原因造成了这一切。Excel VBA似乎没有通常的编译器来捕获源代码中的错误,但我怀疑这可能是VBA代码中的语法错误?非常感谢您的帮助。问题是由于我将模块的名称从默认的Module1
更改为更具信息性的名称所致。尝试从VBA而不是从工作表运行它,以查看问题的原因。(如有必要,添加断点并用F8逐步完成代码。)