xxxRxxx
xxxRxxx

Reputation: 367

Word wrap in Excel VBA?

I'm writing an IF-THEN-ELSE statement with 19 OR statements contained within it. Is there a way to use word wrap to make it easier to see what I'm coding? Or is there some way to make this more doable? It's very difficult to keep track of all of the statements.

Note: I am not trying to use the Wrapandfit() function or any other function to modify cells, I am talking about the VBA window itself.

An example of the statement:

If InStr(1, Cells(i, 1).Value, "Administration") > 0 Or InStr(1, Cells(i, 1), "Administrative") > 0 Or InStr(1, Cells(i, 1), "Administrator") > 0 Or InStr(1, Cells(i, 1), "Assistant") > 0 Or InStr(1, Cells(i, 1), "Coordinator") > 0 Then
Cells(i, 2).Value = "Administrative"

This matters partly because I can't just make everything on a separate line and delete the line breaks later because of the debugger (which also takes extra work to disable).

Upvotes: 2

Views: 4709

Answers (3)

ChrisG
ChrisG

Reputation: 1251

Use the "_" character

IF this
 OR this _ 
 OR this _
 OR this _
 THEN this

Upvotes: 2

ChipsLetten
ChipsLetten

Reputation: 2953

You can always create a separate function and put your 19 OR statements within the function. If you give the function a meaningful name, then your IF statement will become much clearer. (Unless you've got a massively long list of parameters for the function).

Here is an example:

Sub mainCode()

Dim i As Integer

    i = 3

    If theValueIsOk(i) Then
        ' do something
    Else
        ' do something else
    End If

End Sub

Private Function theValueIsOk(theValue As Integer) As Boolean

Dim result As Boolean

    result = False

    If theValue = 0 Then
        result = True
        GoTo Exit_Function
    End If

    If (theValue = 1) Or (theValue = 3) Then
        result = True
        GoTo Exit_Function
    End If

Exit_Function:
    theValueIsOk = result
    Exit Function

End Function

Upvotes: 0

John Coleman
John Coleman

Reputation: 51988

You can use the line continuation character _:

Function SmallPrime(n As Integer) As Boolean
    If n = 2 Or n = 3 Or n = 5 Or n = 7 _
       Or n = 11 Or n = 13 Or n = 17 _
       Or n = 19 Then
        SmallPrime = True
    Else
        SmallPrime = False
    End If
End Function

Note the space before the _

On edit:

If you want genuine word wrap on the editor level, you can keep an open copy of TextPad with wordwrap enabled and the VBA syntax highlighting definitions available from their website and then copy-paste into the VBA editor. (Notepad++ probably has similar functionality, though Textpad is what I am familiar with).

Upvotes: 6

Related Questions