FryGirl
Well-known Member
- Joined
- Nov 11, 2008
- Messages
- 1,368
- Office Version
- 365
- 2016
- Platform
- Windows
I have this code working fine, but would love to turn the if statement case statement into a function. Can somebody show me how to turn the if statement into a function. In essence I'm just trying to compartmentalize the code into small procedures.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Variant: X = Target.Value '<<<<< save the new value
Dim MyCol As Long: MyCol = 26
Dim rng As Range: Set rng = Me.Range("E:F, H:J")
If Intersect(Target, rng) Is Nothing Or Target.CountLarge > 1 Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
.Undo '<<<<< restore the previous value
End With
Target.Value = X '<<<<< restore the new value
[COLOR=#ff0000] If X Like "Per*" Then
Select Case X[/COLOR]
[COLOR=#ff0000] Case "Per Year": X = "PY"
Case "Per Quarter": X = "PQ"
Case "Per Month": X = "PM"
Case "Per Day": X = "PD"[/COLOR]
[COLOR=#ff0000] End Select[/COLOR]
[COLOR=#ff0000] End If[/COLOR]
If Cells(Target.Row, Target.Column + MyCol).Value = "" Then
Cells(Target.Row, Target.Column + MyCol).Value = X '<<<<< old value
ElseIf Cells(Target.Row, Target.Column + MyCol).Value <> "" Then
Cells(Target.Row, Target.Column + MyCol).Value = Cells(Target.Row, Target.Column + MyCol).Value & ", " & X
End If
ThisWorkbook.Worksheets("Sheet1").Range("AE:AF,AH:AJ").EntireColumn.AutoFit
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Last edited: