简体   繁体   中英

Nested If in VBA

I've been tasked to correct someones code in VBA. I've never VBA programmed before so this is very basic.

Am I correct in assuming that after the first Then it checks if the next condition is true and thats where it executes the last line?

    If data.Cells(i, 3 + 4).Value <> "" Then

    If data.Cells(i, 2 + y).Value <> "" Then
        tilqa = data.Cells(i, 2 + y)
    End If

Whenever you have a question about the functioning of a code, try to write a small example, like the one below, with MsgBox() , showing exactly what is happening. The 1=1 and 2=2 is always evaluated to True :

Sub TestMe()

    If 1 = 1 Then
        If 2 = 2 Then
            MsgBox "First check here!"
        Else
            MsgBox "This is not checked!"
        End If
        MsgBox "Then check here!"
    End If

End Sub

Amd this is how the If-Else-End If may function without Else :

Sub TestMe()

    If 1 = 1 Then
        If 2 = 2 Then
            MsgBox "First check here!"
        End If
        MsgBox "Then check here!"
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM