• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Conbining Two worksheet_change macros in one sheet

brijeshkeni

New Member
Dear All,


I was trying to use two worksheet_change macros in one sheet further to which i was getting error. Can you please advise what could be wrong? the macros are defined below.


Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range

Set rng = Target.Parent.Range("E48")

If Target.Count > 1 Then Exit Sub

If Intersect(Target, rng) Is Nothing Then Exit Sub

Select Case Target

Case ""

Rows("49:54").Hidden = True

Case "1"

Rows("50:50").Hidden = False

Rows("51:54").Hidden = True

Rows("55:55").Hidden = False

Case "2"

Rows("50:51").Rows.Hidden = False

Rows("52:54").Rows.Hidden = True

Rows("55:55").Hidden = False

Case "3"

Rows("50:52").Rows.Hidden = False

Rows("53:54").Rows.Hidden = True

Rows("55:55").Hidden = False

Case "4"

Rows("50:53").Rows.Hidden = False

Rows("54:54").Rows.Hidden = True

Rows("55:55").Hidden = False

Case "5"

Rows("50:54").Rows.Hidden = False

Rows("55:55").Hidden = False

End Select

End Sub


Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range

Set rng = Target.Parent.Range("D27")

If Target.Count > 1 Then Exit Sub

If Intersect(Target, rng) Is Nothing Then Exit Sub

Select Case Target

Case ""

Rows("28:33").Hidden = True

Case "1"

Rows("28:29").Hidden = False

Rows("30:33").Hidden = True

Case "2"

Rows("28:30").Rows.Hidden = False

Rows("31:33").Rows.Hidden = True

Case "3"

Rows("28:31").Rows.Hidden = False

Rows("32:33").Rows.Hidden = True

Case "4"

Rows("28:32").Rows.Hidden = False

Rows("33:33").Rows.Hidden = True

Case "5"

Rows("28:33").Rows.Hidden = False

End Select

End Sub
 
Hi Brijesh ,


The problem is you cannot have two macros with the same name in one sheet.


Try the following :

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not (Application.Intersect(Target, Range("E48")) Is Nothing) Then
Select Case Target
Case ""
Rows("49:54").Hidden = True
Case "1"
Rows("50:50").Hidden = False
Rows("51:54").Hidden = True
Rows("55:55").Hidden = False
Case "2"
Rows("50:51").Rows.Hidden = False
Rows("52:54").Rows.Hidden = True
Rows("55:55").Hidden = False
Case "3"
Rows("50:52").Rows.Hidden = False
Rows("53:54").Rows.Hidden = True
Rows("55:55").Hidden = False
Case "4"
Rows("50:53").Rows.Hidden = False
Rows("54:54").Rows.Hidden = True
Rows("55:55").Hidden = False
Case "5"
Rows("50:54").Rows.Hidden = False
Rows("55:55").Hidden = False
End Select

Else
If Application.Intersect(Target, Range("D27")) Is Nothing Then Exit Sub
Select Case Target
Case ""
Rows("28:33").Hidden = True
Case "1"
Rows("28:29").Hidden = False
Rows("30:33").Hidden = True
Case "2"
Rows("28:30").Rows.Hidden = False
Rows("31:33").Rows.Hidden = True
Case "3"
Rows("28:31").Rows.Hidden = False
Rows("32:33").Rows.Hidden = True
Case "4"
Rows("28:32").Rows.Hidden = False
Rows("33:33").Rows.Hidden = True
Case "5"
Rows("28:33").Rows.Hidden = False
End Select
End If
End Sub
[/pre]
Narayan
 
Hi Narayan,


But what if i need to add multiple worksheet_change macros. Can you please advise?

If there any tutorial you can suggest for beginner level to learn this.


Best Regards,

Brijesh
 
Hi Brijesh ,


Use something like the following :

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Select Case Target.Address
Case "$A$1"
' Code here
MsgBox Target & "   - 1"
Case "$D$7"
' Code here
MsgBox Target & "   - 2"
Case "$H$3"
' Code here
MsgBox Target & "   - 3"
End Select
Application.EnableEvents = True
End Sub
[/pre]
The target cells which will trigger the change event code are A1 , D7 and H3 ; you can have many more according to your requirement.


Narayan
 
Hi Narayan,


As advised i tried the following but the target cells did not respond.Can you please advise what could be the reason.


Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

Select Case Target.Address

Case "$E$49"

Case ""

Rows("50:56").Hidden = True

Case "1"

Rows("50:52").Hidden = False

Rows("53:56").Hidden = True

Rows("57:57").Hidden = False

Case "2"

Rows("50:53").Rows.Hidden = False

Rows("54:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case "3"

Rows("50:54").Rows.Hidden = False

Rows("55:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case "4"

Rows("50:55").Rows.Hidden = False

Rows("56:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case "5"

Rows("50:56").Rows.Hidden = False

Rows("57:57").Hidden = False

MsgBox Target & " - 1"

Case "$E$56"

Case ""

Rows("28:34").Hidden = True

Case "1"

Rows("28:35").Hidden = False

Rows("31:34").Hidden = True

Case "2"

Rows("28:35").Rows.Hidden = False

Rows("32:34").Rows.Hidden = True

Case "3"

Rows("28:35").Rows.Hidden = False

Rows("33:34").Rows.Hidden = True

Case "4"

Rows("28:35").Rows.Hidden = False

Rows("34:34").Rows.Hidden = True

Case "5"

Rows("28:35").Rows.Hidden = False

MsgBox Target & " - 2"

End Select

Application.EnableEvents = True

End Sub
 
Try the following:

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$E$49" Then
Select Case Range("$E$49").Value
Case 0
Rows("50:56").Hidden = True
Case 1
Rows("50:52").Hidden = False
Rows("53:56").Hidden = True
Rows("57:57").Hidden = False
Case 2
Rows("50:53").Rows.Hidden = False
Rows("54:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 3
Rows("50:54").Rows.Hidden = False
Rows("55:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 4
Rows("50:55").Rows.Hidden = False
Rows("56:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 5
Rows("50:56").Rows.Hidden = False
Rows("57:57").Hidden = False
MsgBox Target & " - 1"
End Select
ElseIf Target.Address = "$E$56" Then
Select Case Range("$E$56").Value
Case 0
Rows("28:34").Hidden = True
Case 1
Rows("28:35").Hidden = False
Rows("31:34").Hidden = True
Case 2
Rows("28:35").Rows.Hidden = False
Rows("32:34").Rows.Hidden = True
Case 3
Rows("28:35").Rows.Hidden = False
Rows("33:34").Rows.Hidden = True
Case 4
Rows("28:35").Rows.Hidden = False
Rows("34:34").Rows.Hidden = True
Case 5
Rows("28:35").Rows.Hidden = False
MsgBox Target & " - 2"
End Select
End If

End Sub
[/pre]
 
Hi Hui,


Thank you for your valued information. I did try it but it does not work for "$E$56". it does work well for "$E$49". Actually i am trying to add multiple events like as follows. can you advise if this could be possible.


Best Regards,

Brijesh


Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Address = "$E$49" Then

Select Case Range("$E$49").Value

Case 0

Rows("50:56").Hidden = True

Case 1

Rows("50:52").Hidden = False

Rows("53:56").Hidden = True

Rows("57:57").Hidden = False

Case 2

Rows("50:53").Rows.Hidden = False

Rows("54:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case 3

Rows("50:54").Rows.Hidden = False

Rows("55:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case 4

Rows("50:55").Rows.Hidden = False

Rows("56:56").Rows.Hidden = True

Rows("57:57").Hidden = False

Case 5

Rows("50:56").Rows.Hidden = False

Rows("57:57").Hidden = False

MsgBox Target & " - 1"

End Select

ElseIf Target.Address = "$E$56" Then

Select Case Range("$E$56").Value

Case 0

Rows("28:34").Hidden = True

Case 1

Rows("28:35").Hidden = False

Rows("31:34").Hidden = True

Case 2

Rows("28:35").Rows.Hidden = False

Rows("32:34").Rows.Hidden = True

Case 3

Rows("28:35").Rows.Hidden = False

Rows("33:34").Rows.Hidden = True

Case 4

Rows("28:35").Rows.Hidden = False

Rows("34:34").Rows.Hidden = True

Case 5

Rows("28:35").Rows.Hidden = False

MsgBox Target & " - 2"

End Select

ElseIf Target.Address = "$F$74" Then

Select Case Range("$F$74").Value

Case Yes

Rows("81:83").Hidden = False

Case No

Rows("81:83").Hidden = True

MsgBox Target & " - 3"

End Select

End If


End Sub
 
In VBA numbers in quotes like "5" are text

No is a variant variable unless you put it in quotes, "No" is Text


Try the following:

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$E$49" Then
Select Case Range("$E$49").Value
Case 0
Rows("50:56").Hidden = True
Case 1
Rows("50:52").Hidden = False
Rows("53:56").Hidden = True
Rows("57:57").Hidden = False
Case 2
Rows("50:53").Rows.Hidden = False
Rows("54:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 3
Rows("50:54").Rows.Hidden = False
Rows("55:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 4
Rows("50:55").Rows.Hidden = False
Rows("56:56").Rows.Hidden = True
Rows("57:57").Hidden = False
Case 5
Rows("50:56").Rows.Hidden = False
Rows("57:57").Hidden = False
MsgBox Target & " - 1"
End Select
ElseIf Target.Address = "$E$56" Then
Select Case Range("$E$56").Value
Case 0
Rows("28:34").Hidden = True
Case 1
Rows("28:35").Hidden = False
Rows("31:34").Hidden = True
Case 2
Rows("28:35").Rows.Hidden = False
Rows("32:34").Rows.Hidden = True
Case 3
Rows("28:35").Rows.Hidden = False
Rows("33:34").Rows.Hidden = True
Case 4
Rows("28:35").Rows.Hidden = False
Rows("34:34").Rows.Hidden = True
Case 5
Rows("28:35").Rows.Hidden = False
MsgBox Target & " - 2"
End Select
ElseIf Target.Address = "$F$74" Then
Select Case Range("$F$74").Value
Case "Yes"
Rows("81:83").Hidden = False
Case "No"
Rows("81:83").Hidden = True
MsgBox Target & " - 3"
End Select
End If

End Sub
[/pre]
 
Dear Hui,


Thank you very much for your valued expertise. Really appreciate your help. It is working fine now.


Best Regards,

Brijesh
 
Hi Hui,


Just wanted to check again on the same topic...I am actually having an error when i protect the above sheet. it gives me a run-time error '1004'.

it also mentions 'Unable to set the hidden property of the range class'


Can you please advise.

Best Regards,

Brijesh
 
Brijesh


When it stops hit Debug, not stop

Can you tell us what line is highlighted when it stops?
 
Hi Hui,

when i change the value of the E49 to 2 then it highlights the following row.


Rows("50:53").Rows.Hidden = False


when i select 4 it then highlights


Rows("50:55").Rows.Hidden = False


Can you please advise how can this be achieved.


Best Regards,

Brijesh
 
Hi Hui,

when i change the value of the E49 to 2 then it highlights the following row.


Rows("50:53").Rows.Hidden = False


when i select 4 it then highlights


Rows("50:55").Rows.Hidden = False


Can you please advise how can this be achieved.


Best Regards,

Brijesh
 
Hi Hui,

when i change the value of the E49 to 2 then it highlights the following row.


Rows("50:53").Rows.Hidden = False


when i select 4 it then highlights


Rows("50:55").Rows.Hidden = False


Can you please advise how can this be achieved.


Best Regards,

Brijesh
 
Back
Top