Add Condition to Worksheet Change Event

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,539
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I am using the below code in my file...

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count = 1 And Target.Address = Range("C3").Address Then

Select Case Target.Value

Case "1SHOW QUANTITY"
Call one_show_quantity

Case "1SHOW VALUE"
Call one_show_value

Case "1SHOW QUANTITY & VALUE"
Call one_show_quantity_value

Case "2SHOW QUANTITY"
Call two_show_quantity

Case "2SHOW VALUE"
Call two_show_value

Case "2SHOW QUANTITY & VALUE"
Call two_show_quantity_value

End Select

End If

End Sub

I would like to add second criteria in the code.

Cell C3 has numbers
Cell B4 has text (Show Quantity, Show Value, Show Quantity & Value)

I want the code to trigger as soon as value in either cell changes.

Any help would be appreciated.

Regards,

Humayun
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Like this ???

Code:
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B4,C3")) Is Nothing Then Exit Sub
 
Upvote 0
Thanks for the reply Michael

Its giving Compile error: End If without block If

Any Idea ???
 
Upvote 0
Take out the last end if...2nd last line in the macro
 
Upvote 0
Done That.

This time not giving any error but the code is not working.
 
Upvote 0
works for me...so your code is now ??

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B4,C3")) Is Nothing Then Exit Sub
Select Case Target.Value

Case "1SHOW QUANTITY"
Call one_show_quantity

Case "1SHOW VALUE"
Call one_show_value

Case "1SHOW QUANTITY & VALUE"
Call one_show_quantity_value

Case "2SHOW QUANTITY"
Call two_show_quantity

Case "2SHOW VALUE"
Call two_show_value

Case "2SHOW QUANTITY & VALUE"
Call two_show_quantity_value

End Select

End Sub
 
Upvote 0
Thanks for reply Michael,

It is still not working....

I found a solution.....

Here it is.... working just Perfect

Private Sub Worksheet_Change(ByVal Target As Range)


If ActiveSheet.Range("C3") = 1 And Range("B4") = "SHOW QUANTITY" Then
Call one_show_quantity
End If
If ActiveSheet.Range("C3") = 1 And Range("B4") = "SHOW VALUE" Then
Call one_show_value
End If
If ActiveSheet.Range("C3") = 1 And Range("B4") = "SHOW QUANTITY & VALUE" Then
Call one_show_quantity_value
End If
If ActiveSheet.Range("C3") = 2 And Range("B4") = "SHOW QUANTITY" Then
Call two_show_quantity
End If
If ActiveSheet.Range("C3") = 2 And Range("B4") = "SHOW VALUE" Then
Call two_show_value
End If
If ActiveSheet.Range("C3") = 2 And Range("B4") = "SHOW QUANTITY & VALUE" Then
Call two_show_quantity_value
End If
End Sub

The code does trigger as soon as any value changes in the range.

There is one thing i would like to ask that when i select cell C3 to change the value then the selection should remain at cell C3
and same for B4

here is the macro

Sub one_show_quantity()

Application.ScreenUpdating = False
Columns("E:AD").Select
Selection.EntireColumn.Hidden = False
Columns("F:AD").Select
Selection.EntireColumn.Hidden = True

End Sub


Sub one_show_value()

Application.ScreenUpdating = False
Columns("E:AD").Select
Selection.EntireColumn.Hidden = False
Range("E:E,G:AD").Select
Selection.EntireColumn.Hidden = True

End Sub
Sub one_show_quantity_value()

Application.ScreenUpdating = False
Columns("E:AD").Select
Selection.EntireColumn.Hidden = False
Range("G:AD").Select
Selection.EntireColumn.Hidden = True

End Sub

I can write this Range("C3").Select to every macro but the problem is when i select B4 to change then also it will go
to Cell C3.... Is there any way i can tell the code to keep the selection where it is ???
 
Upvote 0
remove all select.Selection from you macros.....like this

Code:
Sub one_show_quantity()

Application.ScreenUpdating = False
Columns("E:AD").EntireColumn.Hidden = False
Columns("F:AD").EntireColumn.Hidden = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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