0

I have a VBA code that's designed to search a CSV String and add Carriage Returns where they should exist. I've split it up into two seperate functions - one to search the string and put the index of where the CRs should go into an array and a second function to actually add the CRs.

The issue I'm running into is that the value in the immediate window/in the watch window for the functions is correct within the function itself, but it assigns the result variable a blank 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

In the function the value is not blank ...but when it passes it back, it says the value is blank

4
  • 1
    Check your spelling: addCarriageReturns vs addCarraigeReturns Commented Jul 26, 2017 at 19:23
  • 1
    Turn on Option Explicit Commented Jul 26, 2017 at 19:24
  • @GordonBell the spelling was it. Thanks. Commented Jul 26, 2017 at 19:28
  • Save yourself the headaches, and never do VBA without "Option Explicit" Commented Jul 26, 2017 at 19:30

1 Answer 1

1

I'm just curious, why do you want separate functions like this?

Can you just use:

line = Replace(line, "SALE", "SALE" & Chr(13))
Sign up to request clarification or add additional context in comments.

2 Comments

Because apparently I'm a masochist and do things the harder way. :) I forgot that replace isn't destructive to the rest of the string, but only affects the part you specify.
Not to worry, happens to all of us. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.