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...

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

<tbody>
</tbody>
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,215,379
Messages
6,124,607
Members
449,174
Latest member
ExcelfromGermany

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