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.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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
 
Upvote 0
If Worksheets("Summary Inbound").Range("A1").Value = "Error"
=IF(COUNTIF('Inbound Detail'!AF1:AH1,"error")>0,"ERROR","Ok")

Some things are case insensitive, in this case, they are case sensitive and vba sees them as unequal strings. That means your statement will always be false.
 
Last edited:
Upvote 0
Hello,

The Exit1 statement will always execute as there is nothing stopping excel executing that bit of code. Excel will just execute all the code top to bottom in a sub-routine. Adding once more exit sub will prevent this:
Code:
If Worksheets("Summary Inbound").Range("A1").Value = "Error" Then GoTo Exit1


Exit Sub


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

Hope that helps
Caleeco
 
Upvote 0
Hello,

The Exit1 statement will always execute as there is nothing stopping excel executing that bit of code. Excel will just execute all the code top to bottom in a sub-routine. Adding once more exit sub will prevent this:
Code:
If Worksheets("Summary Inbound").Range("A1").Value = "Error" Then GoTo Exit1


Exit Sub


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

Hope that helps
Caleeco


That just stopped the code regardless of A1's Value
 
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


i also tried this with no success...

It still "sees" A1 as error regardless...
 
Upvote 0
Rather than picking at code that is known to not do what you want..

Can you explain with words what you actually do want the code to do ?

It sounds like you only want the code after Exit1 to run if the statement is True
This will also address the case sensitivity issue.

Code:
If UCase(Worksheets("Summary Inbound").Range("A1").Value) = "ERROR" Then
    Application.ScreenUpdating = True
    MsgBox "Error", vbCritical
    Exit Sub
End If
 
Last edited:
Upvote 0
Rather than picking at code that is known to not do what you want..

Can you explain with words what you actually do want the code to do ?

It sounds like you only want the code after Exit1 to run if the statement is True
This will also address the case sensitivity issue.

Code:
If UCase(Worksheets("Summary Inbound").Range("A1").Value) = "ERROR" Then
    Application.ScreenUpdating = True
    MsgBox "ERROR", vbCritical
    Exit Sub
End If


Sorry about that but, yes Exactly that

so i have a macro that copies, pastes information accordingly and what i need from his is, IF the information was pasted incorrectly and the formula in A1 will be used to validate this.

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


now, i'm just testing this single "test" macro and it still always displays the error message in the vbexclamation:

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


Additionally, shouldn't the case sensitive issue arise when the condition is actually met?

in other words my problem constantly thinks that the word error is on A1 regardless if it reads OK or ERROR or anything else (as the result of that formula)
 
Last edited:
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.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,523
Messages
6,125,323
Members
449,218
Latest member
Excel Master

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