我有一个VBA代码,用于搜索CSV字符串并添加它们应存在的回车符。我将它分成两个单独的函数 - 一个用于搜索字符串并将CR应该放入数组的索引和第二个用于实际添加CR的函数。
我遇到的问题是函数本身在函数的观察窗口中的立即窗口/值是正确的,但是它为结果变量分配了一个空字符串。
'*****************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
In the function the value is not blank ...but when it passes it back, it says the value is blank
答案 0 :(得分:1)
我只是好奇,为什么你想要这样的单独功能?
你能使用:
line = Replace(line, "SALE", "SALE" & Chr(13))