Questions ArchiveCategory: Questionsvba-project-if-then-else-statements
Bethany asked 2 years ago

I keep getting an error message on my If, then, else code and cannot figure out what is wrong. I have looked up multiple examples and watched several of the videos and still can\\\’t figure out why none of mine will work.  here is one of them: If numHours <= 5 Then OH = 0 Else: OH = numHours – 5 End If

Cindi replied 2 years ago

How are you formatting it? I know for me I had an issue when I put things on the same line or on different lines. Like, for your code it should be written like:
If numHours<=5 Then
OH= numHours-5
End If
For some reason you have to put "If [ ]…Then" on the same line and then hit enter and input what should happen. Also sometimes excel literally doesn't work anymore (I have an old version) and I have to force quit the program and open it again.

1 Answers
Cairo answered 2 years ago

If condition Then [ statements ] [ Else elsestatements ]
Or, you can use the block form syntax:
If condition Then
statements ]
ElseIf condition-n Then
elseifstatements ]]
elsestatements ]]
End If
It is possible that something is wrong and you need spaces inbetween the <= and = and -. Try that, also Googling errors works pretty well usually to debug.
Email if you still have issues!

Your Answer

9 + 11 =