VBA函数不返回值

我有一个VBA代码,旨在search一个CSVstring,并添加它们应该存在的回车。 我已经把它分成两个独立的函数 – 一个searchstring,并把CR的位置索引放入数组,第二个函数实际添加CR。

我遇到的问题是函数的直接窗口中/函数的监视窗口中的值在函数本身中是正确的,但它将结果variables赋值为空string。

'*****************Import CSV********************** 'Took this straight off the internet because it was reading Jet.com files as one single line ' Sub ImportCSVFile(filepath As String) ..... line = SearchString(line, "SALE") ..... End Sub '****************Search String*************************** 'This is search the string for something - It will then call a function to insert carriage returns Function SearchString(source As String, target As String) As String Dim i As Integer Dim k As Integer Dim myArray() As Variant Dim resultString As String Do i = i + 1 If Mid(source, i, Len(target)) = target Then ReDim Preserve myArray(k) myArray(k) = i k = k + 1 End If DoEvents Loop Until i = Len(source) resultString = addCarriageReturns(source, myArray) 'resultString here is assigned a blank string SearchString = resultString End Function '***************Add Carraige Returns************************** 'Cycle through the indices held in the array and place carriage returns into the string Function addCarriageReturns(source As String, myArray As Variant) As String Dim i As Integer Dim resultString As String resultString = source For i = 0 To UBound(myArray, 1) resultString = Left(resultString, myArray(i) + i) & Chr(13) & Right(resultString, Len(resultString) - myArray(i) + i) Next i addCarraigeReturns = resultString 'The value of addCarriageReturn is correct in the immediate window here End Function 

在这个函数中,这个值不是空白的 ,但是当它传递回来的时候,它的值是空的

我只是好奇,你为什么要这样的单独的function?

你可以使用:

 line = Replace(line, "SALE", "SALE" & Chr(13))