Trouble with two Worksheet_Change codes

ryanw1

New Member
Joined
Mar 11, 2019
Messages
7
Good Afternoon,

I'm new to VBA codes and need some help. I would like to have the two code below operating on the same worksheet. I've got the first one working but not sure how to add the second. Some help would be greatly appreciate.

Code 1)

Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$F$21" Or Target.Address = "$E$50" Or Target.Address = "$F$53" Or Target.Address = "$E$56" Or Target.Address = "$E$62" Or Target.Address = "$D$66" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

Code 2)

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then
Select Case Target.Value
Case "Yes"
ActiveSheet.Rows("3:5").Hidden = False
Case "No"
ActiveSheet.Rows("3:5").Hidden = True
End Select
End If

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
.
Not tested here . See if this works for you there.

Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String


Application.EnableEvents = True


On Error GoTo Exitsub


    If Target.Address = "$F$21" Or Target.Address = "$E$50" Or Target.Address = "$F$53" Or Target.Address = "$E$56" Or Target.Address = "$E$62" Or Target.Address = "$D$66" Then
        
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else: If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                    Target.Value = Oldvalue & ", " & Newvalue
                Else:
                    Target.Value = Oldvalue
                End If
            End If
        End If
    End If


    If Target.Address = "$A$1" Then
        Select Case Target.Value
            Case "Yes"
            ActiveSheet.Rows("3:5").Hidden = False
        Case "No"
            ActiveSheet.Rows("3:5").Hidden = True
        End Select
    End If


Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True


End Sub


NOTE: Please place your code example inside the hash marks. The hash symbol (pound symbol) found on the menu when replying.

This is a requirement of this and many other forums.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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