Activate based on Date

Hawk04

New Member
Joined
Apr 1, 2010
Messages
8
Hello: I'm trying to activate a command button on an entry form by determining if the year is the year after the current file was created. The purpose is to allow the user to auto-populate data at the beginning of a new golf season. As I am not very familiar with using dates in VBA I have tried a variety of code examples with all giving me errors. My latest attempt is:

Code:
Sub Start_Year()

    'Code to determine whether cmdStartYear is enabled
    Dim CurrentYear As Date
    Dim YrCreated As Date
    Dim oWB As Workbook
    
    Set oWB = ActiveWorkbook
    
    CurrentYear = Year(Date)
    
    YrCreated = Format(oWB.DateCreated, "yyyy")

    If CurrentYear = YrCreated + 1 Then
        cmdStartYear.Visible = True
        Else: cmdStartYear.Visible = False
    End If

End Sub

All versions tried result in the failure of my entry form to open and this latest version returns an error message "Object doesn't support this property or method" and I feel stupid that I can't see what is causing this.

Thanks in advance for any advice.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
try

Code:
Sub Start_Year()
    'Code to determine whether cmdStartYear is enabled
    Dim CurrentYear As Date
    Dim YrCreated As Date
    Dim oWB As Workbook
 
    Set oWB = ActiveWorkbook
 
    CurrentYear = Year(Date)
 
    YrCreated = Format(oWB.BuiltinDocumentProperties("Creation Date"), "yyyy")
    If CurrentYear = YrCreated + 1 Then
        cmdStartYear.Visible = True
    Else
        cmdStartYear.Visible = False
    End If
End Sub
 
Upvote 0
Here you go:
Code:
Sub Start_Year()

    'Code to determine whether cmdStartYear is enabled
    Dim CurrentYear As Integer
    Dim YrCreated As Integer
    
    Dim oWB As Workbook
    
    Set oWB = ActiveWorkbook
    
    CurrentYear = Format(Date, "yyyy")
    
    YrCreated = Format(oWB.BuiltinDocumentProperties("Creation Date"), "yyyy")
    
    If CurrentYear = YrCreated + 1 Then
        cmdStartYear.Visible = True
        Else: cmdStartYear.Visible = False
    End If

End Sub
 
Upvote 0
Code:
Sub Start_Year()

    'Code to determine whether cmdStartYear is enabled
 
    If Year(Date) = CInt(Format(ActiveWorkbook.BuiltinDocumentProperties("Creation Date"), "yyyy")) + 1 Then
        cmdStartYear.Visible = True
    Else
        cmdStartYear.Visible = False
    End If
    
End Sub
 
Last edited:
Upvote 0
Thanks to all who responded so quickly! I see what I missed and have it working now! You folks are Great!
 
Upvote 0
FYI you don'rt really need the IF statement...

Code:
Sub Start_Year()
    
    cmdStartYear.Visible = Year(Date) = CInt(Format(ActiveWorkbook.BuiltinDocumentProperties("Creation Date"), "yyyy")) + 1
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
Members
452,915
Latest member
hannnahheileen

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