2

I have a method called "checkConvert" which is supposed to return a String.

I have created a For Loop which at the end of the loop returns a value as checkConvert. Also, inside that For Loop, I have setup a Case and inside that Case, another For loop with If Statements inside. Inside those If Statements, I have also tried to return a value as checkConvert, however it does NOT work at all.

For i = val To lastColumn_source
    For j = 1 To 3
        Dim cellValue As String
        cellValue = salesSource.Cells(j, i).Value

        Select Case i
            Case 1
                Dim match As Boolean
                match = False                'Default is false.  
                                             'Equating two cells = false.

                For x = 1 To lastRow_check
                    If cellValue = sourceCheck.Cells(x, 1) Then
                        CRMDest.Cells(j, 2) = newName
                        match = True         'Match is found.  Thus, true.
                    End If
                    If x = lastRow_check And match = False Then
                        checkConvert = newName           'Supposed to return
                                                         'value.  Doesn't
                                                         'work.
                        MsgBox checkConvert              'But MsgBox works.
                    End If
                 Next x
            Case 2                                'Several more cases follow.
            Case 13
                 checkConvert = "End of Program."        'Returns String
                                                         'to "end program."
                                                         'This one works.
        End Select

        checkConvert = "Move."                           'This return also
                                                         'works.
    Next j
Next i

1 Answer 1

4
    checkConvert = newName           'Supposed to return
                                     'value.  Doesn't
                                     'work.
    MsgBox checkConvert              'But MsgBox works.

It looks like you're under the impression that assigning a function's return value immediately returns. That's not how VBA works.

You've assigned a return value, but the code keeps running because, well, an assignment is just that: an assignment.

If you want to exit the function immediately after setting its return value, you need to do so explicitly:

    checkConvert = newName
    Exit Function

The return values that "work", only work out of sheer luck. You're assigning the return value inside a loop, and never jumping out of it, so the value that's returned by the function is whatever value checkConvert was last assigned to, in the last iteration.

Sign up to request clarification or add additional context in comments.

1 Comment

My God. Damn. That makes so much sense. I'll see what happens if I do "Exit Function." Thanks so much, Mat's Mug. Appreciate it. :)

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.