1

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
3
  • 3
    This doesn't throw some kind of syntax error? Commented Oct 24, 2019 at 11:24
  • You can add this line Option Explicit to the top of the VBA file, it will help get compilation errors, See Option Explicit Statement Commented Oct 24, 2019 at 11:32
  • 1
    In the VBA IDE you can place the cursor on any keyword and press F1. This will bring up the MS Help page for that particular keyword. I Commented Oct 24, 2019 at 11:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Can you write a conditional statement without an Else? Because that's what my code does and I'm not sure what's happening
@SaimNazir - I have edited, adding the option without Else. In general, simply give it a few tries, delete a few lines and experiment with the code. This is the best way to learn what is happenning.

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.