2 conditions in Worksheet_Change sub

BMD44

Board Regular
Joined
Sep 25, 2019
Messages
72
Hello,

I have a requirement where in a message needs to be displayed based on the cell value in the sheet which works as expected which is below.

Now, I need another check and message to be displayed based on a different cell value when the value changes which is the second section of the code that doesn't work. Can anyone please suggest how can we have multiple checks in Worksheet_Change Sub.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$2" Then
        If Target.Value = "Yes" Then
            MsgBox "Check required."
        ElseIf Target.Value = "No" Then
            MsgBox "Check not required"
        End If
    End If

  If Target.Address = "$E$2" Then
        If Target.Value = "Yes" Then
            MsgBox "Total > 500"
        ElseIf Target.Value = "No" Then
            MsgBox "Total < 500"
        End If
    End If
   
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Doesn't work means what?
The code looks fine - as long as you want something to happen for Yes or No but only for ranges D2 and E2.
However, you're checking for No and Yes. If the cell contains NO or no and YES or yes then that's probably your issue.
 
Upvote 0
Hello,

I have "Yes" and "No". While for the first scenario, I see a message when the drop down selection is Yes/No. For the second scenario even if the value is Yes or No, I do not see any message. Attaching the screenshot as I am not sure if we can upload file. Thanks.
 

Attachments

  • sample.png
    sample.png
    17.6 KB · Views: 13
Upvote 0
To add, the second scenario works while I have my cursor on cell E2 when I change it to

'Private Sub Worksheet_SelectionChange(ByVal Target As Range)'

But, is there a way I see the message automatically when the value changes in the cell instead of coming up when we select the cell.
 
Upvote 0
Do you by any chance have a formula in E2 ?
If so show us the formula.
A formula will not trigger a worksheet change event
 
Upvote 0
Your code works for me as long as I type in a cell. So as Alex notes, a formula won't trigger it. AFAIK, neither will a combobox change and that's what I see in the pic. I suppose you need to use the combo change event for the second test.
 
Upvote 0
Do you by any chance have a formula in E2 ?
If so show us the formula.
A formula will not trigger a worksheet change event
Hi Alex,

E2 is a formula. It gives value as "Yes" or "No" based on the formula.

=IF(SUM(E6:E500)>50000,"Yes","No")
 
Upvote 0
Your code works for me as long as I type in a cell. So as Alex notes, a formula won't trigger it. AFAIK, neither will a combobox change and that's what I see in the pic. I suppose you need to use the combo change event for the second test.
Hi Micron,

The cell that I am referring to is a formula cell. Can you please let me know how can we use Combo change event.
 
Upvote 0
Is the date in the range E6:E500 manually entered or are they also formulas ?
PS: Your initial post say 500 not 50,000
 
Upvote 0
Is the date in the range E6:E500 manually entered or are they also formulas ?
PS: Your initial post say 500 not 50,000
Hi Alex,

E6:E500 is the list of values which will be updated every month manually. The total of the values should be greater than 50,000. To keep it simple, I have given the total as 500 in m initial post. The actual limit is 50,000.
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,272
Members
449,219
Latest member
daynle

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