Simple VBA question (i think)

deletedalien

Well-known Member
Joined
Dec 8, 2008
Messages
505
Office Version
  1. 2013
Platform
  1. Windows
i have this line of code:

Code:
               If Worksheets("Summary Inbound").Range("A1").Value = "Error" Then GoTo Exit1
Exit1:
              Application.ScreenUpdating = True
               MsgBox "Error", vbCritical
               Exit Sub

and the following formula in Summary inbound A1
=IF(COUNTIF('Inbound Detail'!AF1:AH1,"error")>0,"ERROR","Ok")

and for whatever reason regardless if A1 says "ok" or "error" it always triggers Exit1 sub routine.

What am i doing wrong?

Thanx in advance.
 
if A1 states that there is an error the rest of the code should stop and an error message should popup basically stating that the user did something wrong.

Then 'the rest of the code' belongs 'BETWEEN' the IF and Next1:
Like I showed in Post #2
MsgBox "Hello" is 'the rest of the code' that stops
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You also need an Exit Sub after 'the rest of the code' before Exit1

Code:
If Worksheets("Summary Inbound").Range("A1").Value = "Error" Then GoTo Exit1
MsgBox "Hello"
Exit Sub

Exit1:
              Application.ScreenUpdating = True
               MsgBox "Error", vbCritical
               Exit Sub
 
Upvote 0
VBA code still just executes one line at a time, and in order.

Exit1: isn't a 'Sub'. It's just the next line of code.


The purpose of the Then Goto Exit1 would be to make the code NOT execute something..
That line is going to run no matter what.

Here is a more appropriate usage of it.
Code:
If Worksheets("Summary Inbound").Range("A1").Value = "Error" Then GoTo Exit1
MsgBox "Hello"

Exit1:
              Application.ScreenUpdating = True
               MsgBox "Error", vbCritical
               Exit Sub

The Message Box will be skipped if the condition is true and it then goes to Exit1

This displays both the "hello" message

And the "Error" Message regardless of the result in A1.
 
Upvote 0
The code after Exit1: will execute always regardless of the true/false result of your IF.
Simply because it's the next line of code after the if.
There is nothing telling VBA to 'skip' that code.

ok how can i tell vba to stop or keep going depending on the result of A1
 
Upvote 0
now, i'm just testing this single "test" macro and it still always displays the error message in the vbexclamation: (regardless of value in cell A1 or Case

Code:
Sub test()
 If Worksheets("Summary Inbound").Range("A1").Value = "ERROR" Then GoTo Exit1
Exit1:
              Application.ScreenUpdating = True
               MsgBox "Error", vbCritical
               Exit Sub
End Sub
 
Upvote 0
I figured it out and yes Jonmo1 you where right.

i just didn't catch it at first haha sorry bout that..
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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