我有一個VBA代碼,旨在搜索CSV字符串並將它們應該存在的位置添加回車。我已經將它分成兩個獨立的函數 - 一個用於搜索字符串,並將CR的位置索引放入數組中,第二個函數用於實際添加CR。VBA函數不返回值
我遇到的問題是函數的直接窗口/監視窗口中的值在函數本身內是正確的,但它將結果變量賦給一個空字符串。
'*****************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
檢查拼寫:addCarriageReturns VS addCarraigeReturns –
打開選項顯式 –
@GordonBell拼寫是它。謝謝。 – BobtimusPrime