how to combine two vba code?

mychi11

Board Regular
Joined
May 11, 2020
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
I have two codes in one worksheet.
Both works. Not sure how to combine these two into one. Wonder if anyone could help?
Private Sub Worksheet_Change(ByVal Target As Range)

Rows("54:103").EntireRow.Hidden = False
x = Range("A29").Value
Select Case x
Case "": Rows("54:103").EntireRow.Hidden = True
Case 1: Rows("55:103").EntireRow.Hidden = True
Case 2: Rows("56:103").EntireRow.Hidden = True
Case 3: Rows("57:103").EntireRow.Hidden = True
Case 4: Rows("58:103").EntireRow.Hidden = True
Case 5: Rows("59:103").EntireRow.Hidden = True
Case 6: Rows("60:103").EntireRow.Hidden = True
Case 7: Rows("61:103").EntireRow.Hidden = True
Case 8: Rows("62:103").EntireRow.Hidden = True
Case 9: Rows("63:103").EntireRow.Hidden = True
Case 10: Rows("64:103").EntireRow.Hidden = True
Case 11: Rows("65:103").EntireRow.Hidden = True
Case 12: Rows("66:103").EntireRow.Hidden = True
Case 13: Rows("67:103").EntireRow.Hidden = True
Case 14: Rows("68:103").EntireRow.Hidden = True
Case 15: Rows("69:103").EntireRow.Hidden = True
Case 16: Rows("70:103").EntireRow.Hidden = True
Case 17: Rows("71:103").EntireRow.Hidden = True
Case 18: Rows("72:103").EntireRow.Hidden = True
Case 19: Rows("73:103").EntireRow.Hidden = True
Case 20: Rows("74:103").EntireRow.Hidden = True
Case 21: Rows("75:103").EntireRow.Hidden = True
Case 22: Rows("76:103").EntireRow.Hidden = True
Case 23: Rows("77:103").EntireRow.Hidden = True
Case 24: Rows("78:103").EntireRow.Hidden = True
Case 25: Rows("79:103").EntireRow.Hidden = True
Case 26: Rows("80:103").EntireRow.Hidden = True
Case 27: Rows("81:103").EntireRow.Hidden = True
Case 28: Rows("82:103").EntireRow.Hidden = True
Case 29: Rows("83:103").EntireRow.Hidden = True
Case 30: Rows("84:103").EntireRow.Hidden = True
Case 31: Rows("85:103").EntireRow.Hidden = True
Case 32: Rows("86:103").EntireRow.Hidden = True
Case 33: Rows("87:103").EntireRow.Hidden = True
Case 34: Rows("88:103").EntireRow.Hidden = True
Case 35: Rows("89:103").EntireRow.Hidden = True
Case 36: Rows("90:103").EntireRow.Hidden = True
Case 37: Rows("91:103").EntireRow.Hidden = True
Case 38: Rows("92:103").EntireRow.Hidden = True
Case 39: Rows("93:103").EntireRow.Hidden = True
Case 40: Rows("94:103").EntireRow.Hidden = True
Case 41: Rows("95:103").EntireRow.Hidden = True
Case 42: Rows("96:103").EntireRow.Hidden = True
Case 43: Rows("97:103").EntireRow.Hidden = True
Case 44: Rows("98:103").EntireRow.Hidden = True
Case 45: Rows("99:103").EntireRow.Hidden = True
Case 46: Rows("100:103").EntireRow.Hidden = True
Case 47: Rows("101:103").EntireRow.Hidden = True
Case 48: Rows("102:103").EntireRow.Hidden = True
Case 49: Rows("103:103").EntireRow.Hidden = True
Case 50: Rows("103:104").EntireRow.Hidden = False
End Select


Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String

On Error GoTo Exitsub
If Target.Address = "$C$2" 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
Target.Value = Oldvalue & ", " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
see if this works
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Oldvalue As String, Newvalue As String
   
    'On Error GoTo Exitsub
    Application.ScreenUpdating = False
    If Target.Address = "$C$2" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Or Target.Value = "" Then
            Application.EnableEvents = True
            Exitsub
        Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
            Else
                Target.Value = Oldvalue & ", " & Newvalue
            End If
        End If
        Application.EnableEvents = True
    Else
        Rows("54:103").EntireRow.Hidden = False
        x = Range("A29").Value
        Select Case x
            Case ""
                Rows("54:103").EntireRow.Hidden = True
            Case 1 To 49
                Rows(54 + x & ":103").EntireRow.Hidden = True
            Case 50
                Rows("103:104").EntireRow.Hidden = False
        End Select
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
thank you for your reply
There is an error with the exitsub where sub or function not defined
 
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