VBA to run routine based on month in corresponding cell

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
417
Office Version
  1. 2007
Platform
  1. Windows
Hello,

I’m looking for a VBA routine that checks if the month of a date in the corresponding row is December; if so, run another routine. Column “C” contain dates; Column “M” is where the entry to be checked will be entered. I assume I want to place the code in the Worksheet_SelectionChange sub.

As example – if an entry is made in cell M44, then check if the month of the date in C44 is 12 (i.e., December). If it is, then run a subroutine called DistributePITI().

Thanks for viewing,
Steve K.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Column C is dates with a long formula:

=IF(B36="","",IF(OR(periods_per_year=26,periods_per_year=52),IF(periods_per_year=26,IF(B36=1,fpdate,C35+14),IF(periods_per_year=52,IF(B36=1,fpdate,C35+7),"n/a")),IF(periods_per_year=24,DATE(YEAR(fpdate),MONTH(fpdate)+(B36-1)/2+IF(AND(DAY(fpdate)>=15,MOD(B36,2)=0),1,0),IF(MOD(B36,2)=0,IF(DAY(fpdate)>=15,DAY(fpdate)-14,DAY(fpdate)+14),DAY(fpdate))),IF(DAY(DATE(YEAR(fpdate),MONTH(fpdate)+B36-1,DAY(fpdate)))<>DAY(fpdate),DATE(YEAR(fpdate),MONTH(fpdate)+B36,0),DATE(YEAR(fpdate),MONTH(fpdate)+B36-1,DAY(fpdate))))))

under the current data, cell C36 has a date of 2/1/2025; C35=1/1/2025; C37=3/1/2025; etc.
 
Upvote 0
See if this makes any difference (I added another level of checking):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim rng As Range
    
'   Set up error handling
    On Error GoTo err_chk
   
'   Check for entry made to column M
    Set rng = Intersect(Target, Columns("M:M"))
   
'   If not entry made in column M, exit
    If rng Is Nothing Then Exit Sub
   
'   Loop through new entries made in column M
    For Each cell In rng
'       Check month of date in column C
        If (cell.Offset(0, -10) <> "") And IsDate(cell.Offset(0, -10)) Then
            If Month(cell.Offset(0, -10)) = 12 Then
'               Call procedure
                Call DistributePITI
            End If
        End If
    Next cell
    
    Exit Sub
    
'error handling
err_chk:
    MsgBox Err.Number & ":" & Err.Description & vbCrLf & "Error happening on row: " & cell.Row

End Sub

If you still get the error, temporarily comment out your procedure call line, and add a message box like this (changes in red):
Rich (BB code):
'       Check month of date in column C
        If (cell.Offset(0, -10) <> "") And IsDate(cell.Offset(0, -10)) Then
            If Month(cell.Offset(0, -10)) = 12 Then
'               Call procedure
                'Call DistributePITI
                MsgBox "Condition is met!"
            End If
        End If

If that runs without error, that tells me the error is probably with your "DistributePITI" procedure. I don't think you posted the code for that, so I am not sure what that is doing.
 
Upvote 0
Solution
See if this makes any difference (I added another level of checking):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim rng As Range
   
'   Set up error handling
    On Error GoTo err_chk
  
'   Check for entry made to column M
    Set rng = Intersect(Target, Columns("M:M"))
  
'   If not entry made in column M, exit
    If rng Is Nothing Then Exit Sub
  
'   Loop through new entries made in column M
    For Each cell In rng
'       Check month of date in column C
        If (cell.Offset(0, -10) <> "") And IsDate(cell.Offset(0, -10)) Then
            If Month(cell.Offset(0, -10)) = 12 Then
'               Call procedure
                Call DistributePITI
            End If
        End If
    Next cell
   
    Exit Sub
   
'error handling
err_chk:
    MsgBox Err.Number & ":" & Err.Description & vbCrLf & "Error happening on row: " & cell.Row

End Sub

If you still get the error, temporarily comment out your procedure call line, and add a message box like this (changes in red):
Rich (BB code):
'       Check month of date in column C
        If (cell.Offset(0, -10) <> "") And IsDate(cell.Offset(0, -10)) Then
            If Month(cell.Offset(0, -10)) = 12 Then
'               Call procedure
                'Call DistributePITI
                MsgBox "Condition is met!"
            End If
        End If

If that runs without error, that tells me the error is probably with your "DistributePITI" procedure. I don't think you posted the code for that, so I am not sure what that is doing.

Joe, I think you got it. I ran a couple tests and all looks good. I have more testing to do but this looks really promising. For now, I will call this SOLVED.

Thank you so much Joe for your time and consideration. You guys and gals are so amazing and helpful.
Again - much appreciated,
Steve K.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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