if - else - then - elseif vba code

MrSak87

New Member
Joined
Jan 8, 2015
Messages
44
Hi all,

I'm having a bit of a night mare. with some simple code that you lot will no doubt be able to fix instantly. The code is simple:

Dim time As Integer, result As String


time = Range("K3").Value
If time > Range("k15") Then MsgBox "K3 value exceeds total time of test. Lower value"

time = Range("K4").Value
If time > Range("k15") Then MsgBox "K4 value exceeds total time of test. Lower value"


time = Range("K5").Value
If time > Range("k15") Then MsgBox "K5 value exceeds total time of test. Lower value"


time = Range("K6").Value
If time > Range("k15") Then MsgBox "K6 value exceeds total time of test. Lower value"


time = Range("K7").Value
If time > Range("k15") Then MsgBox "K7 value exceeds total time of test. Lower value"


time = Range("K8").Value
If time > Range("k15") Then MsgBox "K8 value exceeds total time of test. Lower value"


time = Range("K9").Value
If time > Range("k15") Then MsgBox "K9 value exceeds total time of test. Lower value"


time = Range("K10").Value
If time > Range("k15") Then MsgBox "K10 value exceeds total time of test. Lower value"

time = Range("K14").Value
If time > Value = 500 Then MsgBox "K14 value exceeds recommended lag limit. Lower value"

I want the sub to exit if the conditions are met. i.e if the msgbox is activated then the sub exits and if the msgbox is not activated to carry on.

I've looked everywhere but keep getting errors something like "can't block if without if" and others.

Please help!

Thank you for your time
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Have a go with this:

Code:
Sub Macro2()

Dim time As Integer

For Each cell In Range("K3:K10")
    time = cell.Value
    If time > Range("K15") Then MsgBox cell.Address(0, 0) & " value exceeds total time of test. Lower value"
Next cell

time = Range("K14").Value
If time > 500 Then MsgBox "K14 value exceeds recommended lag limit. Lower value"

End Sub
 
Upvote 0
Cheers for your reply Steve. Your code is 1000 times better than mine and works perfectly, I've altered it so it's the same as yours. However I have a huge macro underneath which I want to run only if any of the message boxes don't appear.

So say on the last one:

time = Range("K14").Value
If time > 500 Then MsgBox "K14 value exceeds recommended lag limit. Lower value"

if the time is in fact over 500 then the sub will exit. If the time value is below 500 the sub will continue as normal?
 
Upvote 0
This will stop the macro once a message box has appeared.

Code:
Sub Macro2()

Dim time As Integer

For Each cell In Range("K3:K10")
    time = cell.Value
    If time > Range("K15") Then
        MsgBox cell.Address(0, 0) & " value exceeds total time of test. Lower value"
        Exit Sub
    End If
Next cell

time = Range("K14").Value
If time > 500 Then
    MsgBox "K14 value exceeds recommended lag limit. Lower value"
    Exit Sub
End If

MsgBox "Continue"

End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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