Multiple IF Statements in a WorkSheet Change

Anfinsen

Board Regular
Joined
Apr 11, 2023
Messages
144
Office Version
  1. 365
Platform
  1. Windows
I know the answer is likely a simple one, but I have been staring at this for long enough to no avail.
My first iF statement works beautifully, but the 2nd and 3rd are disregarded. I am thinking I need Elseifs, but they aren't playing nice!
Any help would be greatly appreciated!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'checks for valid part #
If IsEmpty(Range("B1")) = False And Range("G1") = 1 Then
MsgBox "The Part you have entered is invalid, please write down the part #, Qty and Location for JD to research further", vbOKOnly
Range("B1").ClearContents

Exit Sub
'checks for valid quantity
If IsEmpty(Range("B2")) = False And Range("G2") = False Then
MsgBox "You have entered an invalid quantity, please try again", vbOKOnly
Range("B2").ClearContents

Exit Sub
'if part and quantity are valid, calls add to inventoryt sub
If IsEmpty(Range("B1")) = False And Range("G1") <> 1 And IsEmpty(Range("B2")) = False And Range("G2") = True Then

Sheets("Control Data").Range("B1:B3").ClearContents

Call Sheet1.AddtoInv

Exit Sub

End If
End If
End If

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It think it is because of all your "Exit Sub" statements.

Please explain the relationship of your three checks (IFs) with each other.
If you want all three checked no matter what, remove the "Exit Subs" and have each IF...END IF separate from the other (and not imbedded).
 
Upvote 0
Solution
Joe,
you are exactly right, the exit subs were the problem. I removed those and it works!
Thank you!
 
Upvote 0
You are welcome.

You only put "Exit Subs" in the middle of your code if you want it to stop there and not do anything after that, if you are within that particular IF block.
 
Upvote 0
You are welcome.

You only put "Exit Subs" in the middle of your code if you want it to stop there and not do anything after that, if you are within that particular IF block.
Joe,
Here is the corrected code, should someone, someday need to see your solution.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'checks for valid part #
If IsEmpty(Range("B1")) = False And Range("G1") = "ERROR" Then
MsgBox "The Part you have entered is invalid, please write down the part #, Qty and Location for JD to research further", vbOKOnly
Range("B1").ClearContents
Range("B1").Select
End If

'checks for valid quantity
If IsEmpty(Range("B2")) = False And Range("G2") = False Then
MsgBox "You have entered an invalid quantity, please try again", vbOKOnly
Range("B2").ClearContents
Range("B2").Select
End If
'if part and quantity are valid, calls add to inventoryt sub
If IsEmpty(Range("B1")) = False And Range("G1") <> "ERROR" And IsEmpty(Range("B2")) = False And Range("G2") = True Then

Sheets("Control Data").Range("B1:B3").ClearContents

Call Sheet1.AddtoInv

End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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