在两个相同的VBA函数中,突然返回#NAME错误

我刚刚经历了一个macros的突然失败。 它调用以下函数,它使用选定的可选定界符连接一系列单元格:

Public Function MAKELIST(ByVal cellRange As Range, Optional ByVal delimiter As String) 'Function to join a range of cells together with an optional Dim c As Range Dim newText As String Dim Count As Integer Count = 0 newText = "" For Each c In cellRange Count = Count + 1 newText = newText & c.Value If Count < cellRange.Count Then newText = newText & delimiter End If Next MAKELIST = newText End Function 

它只是将手动input的单元格数据连接在一起 – 任何值都似乎正在破坏它。 看起来问题在于函数是如何被引用/被调用的(对不起,命名不好)而不是函数本身。

完美的工作。 我在文件夹之间移动文件,它突然停止工作,每次都返回#NAME错误。 代码中没有任何改变,所以我将它从MAKELIST改为MAKELIST2 ,使用相同的VBA。 这完美的作品。 但是,我显然不希望在工作簿中更改对函数的每一个引用,并且我希望它具有强健性和未来性,所以这不会发生在其他用户身上。 谁能解释为什么会发生这种情况?

谢谢!

看到有问题的范围将是有用的。 虽然你可以使用这个更短的function

[已更新以处理多列范围]

 Public Function MAKELIST2(ByVal cellRange As Range, Optional delimiter As String) Dim rng1 As Range For Each rng1 In cellRange.Columns MAKELIST2 = MAKELIST2 & Join(Application.Transpose(rng1), delimiter) Next End Function