Results 1 to 5 of 5

VBA nested if

This is a discussion on VBA nested if within the Excel Questions forums, part of the Question Forums category; Hi can I rewite this better? Code: If Flag = True Then _ If vCode "" Then _ If lCodeV ...

  1. #1
    Board Regular
    Join Date
    Oct 2008
    Location
    Bedfordshire (UK)
    Posts
    966

    Default VBA nested if

    Hi can I rewite this better?

    Code:
    If Flag = True Then _
                If vCode <> "" Then _
                    If lCodeV >= 0 Then _
                        lCodeVTemp = 0
                        
     If Flag = True Then _
                If vCode <> "" Then _
                    If lCodeV < 0 Then _
                        lCodeVTemp = lCodeV
    why can i not put:
    Code:
    If Flag = True And vCode <> "" and lCodeV >= 0 Then _
          lCodeVTemp = 0
    ElseIf Flag = True And vCode <> "" and lCodeV < 0 Then _
          lCodeVTemp = lCodeV
    EndIf
    I get error message "else without if"

  2. #2
    VoG
    VoG is offline
    MrExcel MVP
    Moderator
    VoG's Avatar
    Join Date
    Jun 2002
    Location
    127.0.0.1
    Posts
    63,537

    Default Re: VBA nested if

    Try removing the continuation characters (_)

    Code:
    If Flag = True And vCode <> "" And lCodeV >= 0 Then
          lCodeVTemp = 0
    ElseIf Flag = True And vCode <> "" And lCodeV < 0 Then
          lCodeVTemp = lCodeV
    End If
    HTH, Peter
    Please test any code on a copy of your workbook.

  3. #3
    MrExcel MVP
    Like totally RAD man
    Colin Legg's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    3,411

    Default Re: VBA nested if

    Hi Martin,

    You need to lose the line continuation characters highlighted in red:
    Code:
    If Flag = True And vCode <> "" and lCodeV >= 0 Then _
          lCodeVTemp = 0
    ElseIf Flag = True And vCode <> "" and lCodeV < 0 Then _
          lCodeVTemp = lCodeV
    EndIf

    Or, here's another way of writing it:
    Code:
        If Flag Then
            If vCode <> "" Then
                If lCodeV >= 0 Then
                    lCodeVTemp = 0
                Else
                    lCodeVTemp = lCodeV
                End If
            End If
        End If
    Hope that helps...

  4. #4
    New Member ketsat's Avatar
    Join Date
    Sep 2009
    Location
    Norway
    Posts
    9

    Default Re: VBA nested if

    You can also change it to two separate single line IFs
    Code:
    If Flag = True And vCode <> "" and lCodeV >= 0 Then _
          lCodeVTemp = 0
    If Flag = True And vCode <> "" and lCodeV < 0 Then _
          lCodeVTemp = lCodeV

  5. #5
    Board Regular
    Join Date
    Oct 2008
    Location
    Bedfordshire (UK)
    Posts
    966

    Default Re: VBA nested if

    thanks guys

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com