how to combine different codes together

mychi11

Board Regular
Joined
May 11, 2020
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
I have three codes, which works independently. but when i put them together only one of the codes work. I have limited knowledge in these. Mostly, I just google and try to edit from others code to fit my purpose. I was wondering if anyone can help for my case. Your help is very much appreciated. thank you.



Private Sub Worksheet_Change(ByVal Target As Range)

AddDate Target

Addc Target

Dropdown Target

End Sub



Sub AddDate(Target As Range)

Application.EnableEvents = False

If Target.Column = 2 Then

Select Case UCase(Target.Value)

Case "2"

Target.Value = "2PN"

Case "1"

Target.Value = "1PN"

Case "0"

Target.Value = "0PN"

Case "3"

Target.Value = "3PN"

Case "M"

Target.Value = "MI"

Case "G"

Target.Value = "GV"

End Select

End If

Application.EnableEvents = True

End Sub

Sub Addc(Target As Range)

Application.EnableEvents = False

If Target.Column = 3 Then

Select Case UCase(Target.Value)

Case "1"

Target.Value = "1C"

Case "2"

Target.Value = "2C"

End Select

End If

Application.EnableEvents = True

End Sub





Sub Dropdown(Target As Range)

Dim Oldvalue As String, Newvalue As String



'On Error GoTo Exitsub

Application.ScreenUpdating = False



If Target.Address = "$B$9" Then

If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Or Target.Value = "" Then

Application.EnableEvents = True

Exit Sub

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("24:73").EntireRow.Hidden = False

x = Range("B14").Value

Select Case x



Case ""

Rows("24:73").EntireRow.Hidden = True

Case 1 To 49

Rows(24 + x & ":73").EntireRow.Hidden = True

Case 50

End Select



End If



Application.ScreenUpdating = True



End Sub





` 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 = "$B$9" Then

If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Or Target.Value = "" Then

Application.EnableEvents = True

Exit Sub

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("24:73").EntireRow.Hidden = False

x = Range("B14").Value

Select Case x



Case ""

Rows("24:73").EntireRow.Hidden = True

Case 1 To 49

Rows(24 + x & ":73").EntireRow.Hidden = True

Case 50

End Select



End If



Application.ScreenUpdating = True



End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Next time, try to put the code within code vba quotation (like below code)
Addc and Droppdown are same target in cell B9
the Dropdown will negates the Addc

sub AddDate & Addc could be combine into one, like this:
VBA Code:
Application.EnableEvents = False
Application.ScreenUpdating = False
If Target.Column = 2 And Target.Count = 1 Then
    With Target
        Select Case UCase(.Value)
            Case "M":           .Value = "MI"
            Case "G":           .Value = "GV"
            Case 0, 1, 2, 3, 4: .Value = .Value & "PN"
        End Select
    End With
End If
If Target.Column = 3 And Target.Count = 1 Then
    With Target
        Select Case .Value
            Case 1, 2: .Value = .Value & "C"
        End Select
    End With
End If
Application.EnableEvents = True
Application.ScreenUpdating = true
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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