Calling a function/sub

KarimK9

New Member
Joined
Feb 16, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I've a code that does some calculations and changes to cells when specific row changes are detected. Here is the Code:
VBA Code:
                Target.Formula = "=IF(" & CLng(Target.Value) & " < TODAY()," & _
                "DATEDIF(" & CLng(Target.Value) & ",TODAY(),""d"") & "" Day(s) Ago""," & _
                "DATEDIF(TODAY()," & CLng(Target.Value) & ",""d"") & "" Day(s) Ahead"")"
The problem is that I want to use that code in multiple areas when certain conditions are met. Currently I've that code pasted 4 times and it works, but creates redundancy. I want to create a (Sub/Function) to have this code and call it whenever the conditions are met. For example:
VBA Code:
Private Sub Calculate_Date(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("L:L")) Is Nothing Then
        With ActiveWorkbook.Worksheets("Interested")
            Application.EnableEvents = False
            >>> Call The Code Here <<<
            Application.EnableEvents = True
        End With
    End If
I tried to create a sub and call it but nothing changes, it doesn't call the sub at all.

Many Thanks!.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Procedures that are called by other procedures must be installed in one of the public code modules (the ones you insert like Module1). You cannot call a Private Sub, however, a Priviate Sub can call any sub procedure from a public code module. The called procedure must have a title line and End Sub line, Assume you have this code in the 'ThisWorkbook' code module.
Code:
Private Sub Workbook_Open()
myCalledProcedure
End Sub
Then in a public code mocule you have
Code:
Sub myCalledPocedure()
MsgBox "Hello There!"
End Sub
Then everytime you open that workbook it would display "Hello There" in a message box generated by the called procedure.

The use of Private in front of a procedure name should be limited to code in worksheet code modules, UserForm code modules and the ThisWorkbook code module. Using it anywhere else can get undesired results, since it implies certain restrictions within the VBA underlying application.
 
Last edited:
Upvote 0
Procedures that are called by other procedures must be installed in one of the public code modules (the ones you insert like Module1). You cannot call a Private Sub, however, a Priviate Sub can call any sub procedure from a public code module. The called procedure must have a title line and End Sub line, Assume you have this code in the 'ThisWorkbook' code module.
Code:
Private Sub Workbook_Open()
myCalledProcedure
End Sub
Then in a public code mocule you have
Code:
Sub myCalledPocedure()
MsgBox "Hello There!"
End Sub
Then everytime you open that workbook it would display "Hello There" in a message box generated by the called procedure.

Okay, I did the exact same thing and it didnt work.

Now I found out why and I feel really embarrassed, for some reason suddenly excel decided to disable VBA Macros, I enabled it and it works perfectly now. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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