VBA: Turn vba if statement into function

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,366
Office Version
  1. 365
  2. 2016
Platform
  1. 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:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Something like this. I have the main sub below for testing.

Code:
Sub main()
Dim X As Variant: X = "Per Day"
X = xAdd(X)
Debug.Print X

End Sub

Function xAdd(X As Variant) As Variant
If X Like "Per*" Then
   Select Case X
       Case "Per Year": X = "PY"
       Case "Per Quarter": X = "PQ"
       Case "Per Month": X = "PM"
       Case "Per Day": X = "PD"
   End Select
End If

xAdd = X
End Function
 
Upvote 0
Here is another function that you can consider...
Code:
Function xAdd(ByVal X As Variant) As String
  If InStr("|Per Year|Per Month|Per Quarter|Per Year|", "|" & X & "|") Then xAdd= "P" & Mid(X, 5, 1)
End Function
 
Last edited:
Upvote 0
Hi Mike, can you elaborate here. I'm not sure I understand.
 
Upvote 0
Hi Mike, can you elaborate here. I'm not sure I understand.
Mike was referring to this part from the code you posted...
Code:
        [B][COLOR="#FF0000"]If X Like "Per*" Then[/COLOR][/B]
            Select Case X
                Case "Per Year": X = "PY"
                Case "Per Quarter": X = "PQ"
                Case "Per Month": X = "PM"
                Case "Per Day": X = "PD"
            End Select
         [B][COLOR="#FF0000"]End If[/COLOR][/B]
You do not need the If..Then statement nor its companion End If statement. Your If..Then statement is filtering for the word "Per", but if you passed the value in X that did not contain that word, the Select Case statement would not be adversely affected as none of its Case blocks would react to it... the non-Per text would simply be ignored, so there is no reason to pre-filter the value of X beforehand.
 
Upvote 0

Forum statistics

Threads
1,223,532
Messages
6,172,875
Members
452,486
Latest member
standw01

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