If/Else error in VBA code (Else with out If error)

Chelsea0270516

New Member
Joined
Oct 15, 2015
Messages
32
Hello! I am currently working through some tutorials on youtube to help me figure out the syntax for VBA. While doing this I hit a road block - I am getting an error BUT I (think anyway) am following along exactly as the video does. And it works in the video.

If the cells A7 & B7 are empty the code should prompt the user to fill them out. But I get an Compile error: Else without If. BUT this code matches exactly what the video is doing...(minus the reference cells but I have that part down pat and the error isn't related to that)

Am I missing something? I just started using the _ so that could be wrong...

[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD]Sub tutorial17()
If ThisWorkbook.Sheets("Sheets2").Range("A7").Value = "" _
Or ThisWorkbook.Sheets("Sheets2").Range("B7").Value = "" _
Then MsgBox "Please fill in all details."
Else

End If


End Sub
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Just move MsgBox "Please fill in all details." to the next line. The format for an if then in Excel is:

If something then
do stuff
else
do other stuff
end if

And the line breaks are important. Also, I think you may have to change the reference to "Sheets2" to "Sheet2". (That is the default name created by Excel, unless you changed it)
 
Upvote 0
Maybe:
Code:
Sub tutorial17()
    If ThisWorkbook.Sheets("Sheets2").Range("A7").Value = "" _
    Or ThisWorkbook.Sheets("Sheets2").Range("B7").Value = "" Then
       MsgBox "Please fill in all details."
    Else
    
    End If
 End Sub
 
Upvote 0
Just move MsgBox "Please fill in all details." to the next line. The format for an if then in Excel is:

If something then
do stuff
else
do other stuff
end if

And the line breaks are important. Also, I think you may have to change the reference to "Sheets2" to "Sheet2". (That is the default name created by Excel, unless you changed it)

Can you just teach everything language wise in VBA like that? The way you explained it made a lot more sense for my brain than most things I found online. Thanks! It works (I fixed the sheets typo & moved the msg box :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top