Worksheet Change Event to Command Button

dhosi439

Board Regular
Joined
May 13, 2009
Messages
62
I use the sub below to check the date entered in a cell, then input a due code two cells to the left of the date cell. This occurs over the 12 months in year, three columns per month, starting in column F, row 5 and continuing to column AO.

This code works perfectly when inputting a date, however I cannot determine how to rewrite the code to work from a command button. What I want is a command button that will cycle through all the due dates and apply the new due code.

The function below is used to determine the starting date for interval one, which starting tomorrow will be the 16th to the 30th, then interval two would be the 30th to the 14th of October. Adding 14 days to each interval, but always starting with the last day of the previous interval. This continues up until the eighth interval or 112 days after the start of the first interval.

Note that I also have a statement to mark due codes of FDue if th date is over the 112 days or the eighth interval.

Also note that this will be run on the first day of the second interval and all 1Due code will be changes to Paid. So anything marked as Paid will need to be disregarded. So all 2Due will need to be marked as 1Due, 3Due as 2Due and so on, until the eighth interval, there is no 9Due to change to 8Due so the dates will need to be checked in some way to check for dates between 98 days and 112 days after the new 1Due or the old 2Due interval.

I also have named ranges setup for the due dates, BillDue, and for the due status, BillStatus. If this helps.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim cell As Range

'Based on Date, Status field populates
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("BillDue")) Is Nothing Then
For Each cell In Target
    If (IsDate(Target)) Then
        cell.Font.Color = RGB(0, 0, 0)
        cell.NumberFormat = "mm/d/yyyy"
        
        If (Target < PayDue Or Target.Offset(0, -2) = "Paid") Then
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue And Target < PayDue + 14) Then
            cell.Offset(0, -2).Value = "1Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 14 And Target < PayDue + 28) Then
            cell.Offset(0, -2).Value = "2Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 28 And Target < PayDue + 42) Then
            cell.Offset(0, -2).Value = "3Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 42 And Target < PayDue + 56) Then
            cell.Offset(0, -2).Value = "4Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 56 And Target < PayDue + 70) Then
            cell.Offset(0, -2).Value = "5Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 70 And Target < PayDue + 84) Then
            cell.Offset(0, -2).Value = "6Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 84 And Target < PayDue + 98) Then
            cell.Offset(0, -2).Value = "7Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= PayDue + 98 And Target < PayDue + 112) Then
            cell.Offset(0, -2).Value = "8Due"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        ElseIf (Target >= 112) Then
            cell.Offset(0, -2).Value = "FDue"
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
        Else
            cell.Offset(0, -2).NumberFormat = "@"
            cell.Offset(0, -2).Font.Color = RGB(255, 0, 0)
        End If
        
    End If
Next cell
End If

ws_exit:
Application.EnableEvents = True

End Sub

Code:
Function PayDue()
FirstPayDate = DateValue("7 Jan 2010") 
PayDue = FirstPayDate + Int((Date - FirstPayDate) / 14) * 14
End Function

Any assistance would be greatly appreciated.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The Change event uses a range object, Target, that is not passed to a routine that is called by a button. Perhaps if you substituted ActiveCell for Target throughout the code, it would do what you want, when called by a command button.
 
Upvote 0
I don't think ActiveCell will work, because there are several cells that need to be checked, there are 30 rows in this table, and 24 of 36 columns that need to be checked and updated.
 
Upvote 0
I was able to rework the code to work for a single cell. What I want is to select a range and when I click the update command button, the code will loop through all the cells and update the offset data.

Code:
If (IsDate(ActiveCell)) Then
    ActiveCell.Font.Color = RGB(0, 0, 0)
    ActiveCell.NumberFormat = "mm/d/yyyy"
    
    If (ActiveCell < PayDue Or ActiveCell.Offset(0, -2) = "Paid") Then
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue And ActiveCell < PayDue + 14) Then
        ActiveCell.Offset(0, -2).Value = "1Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 14 And ActiveCell < PayDue + 28) Then
        ActiveCell.Offset(0, -2).Value = "2Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 28 And ActiveCell < PayDue + 42) Then
        ActiveCell.Offset(0, -2).Value = "3Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 42 And ActiveCell < PayDue + 56) Then
        ActiveCell.Offset(0, -2).Value = "4Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 56 And ActiveCell < PayDue + 70) Then
        ActiveCell.Offset(0, -2).Value = "5Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 70 And ActiveCell < PayDue + 84) Then
        ActiveCell.Offset(0, -2).Value = "6Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 84 And ActiveCell < PayDue + 98) Then
        ActiveCell.Offset(0, -2).Value = "7Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= PayDue + 98 And ActiveCell < PayDue + 112) Then
        ActiveCell.Offset(0, -2).Value = "8Due"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell >= 112) Then
        ActiveCell.Offset(0, -2).Value = "FDue"
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    ElseIf (ActiveCell.Offset(0, -2) = "Rdue") Then
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(0, 0, 0)
    Else
        ActiveCell.Offset(0, -2).NumberFormat = "@"
        ActiveCell.Offset(0, -2).Font.Color = RGB(255, 0, 0)
    End If
End If

Any suggestions?
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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