Error Creating Cell Validation List Using a Named Range in VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
When I execute the code below, I get this error with the line highlight in red:
"Application-defined or object-defined error"

I am hoping someone can let me know where I may have went wrong with my code.

Rich (BB code):
Private Sub Workbook_Open()
    Stop
    declarations
    With Worksheets("GUI")
        .Unprotect
        With .Range("A1")
            .Locked = False
            .Interior.Color = RGB(169, 208, 142) 'green
            .Borders.Color = RGB(55, 86, 35)
            .Font.Color = RGB(55, 86, 35)
        End With
        'determine tomorrow's date to autopopulate user query date fields
        tomorrow = Date + 1
        tm_day = Day(tomorrow)
        tm_month = Month(tomorrow)
        tm_yr = Year(tomorrow)
        'leap year
        If Int((tm_yr / 4)) = (tm_yr / 4) Then
            usr_lp_yr = True
        Else
            usr_lp_yr = False
        End If
        MsgBox "LEAP Year : " & usr_lp_yr
        
        'month value
        tm_mnth = Month(tomorrow)
        'leap year
        If tm_month = 2 Then
            If usr_lp_yr = True Then
                MsgBox "Leap Year. February has 29 Days"
                .Range("D4").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=lyfeb"
            Else
                MsgBox "Not a leap year. February has 28 days."
                .Range("D4").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=nlyfeb"
            End If
        ElseIf tm_month = 1 Or tm_month = 3 Or tm_month = 5 Or tm_month = 7 Or tm_month = 8 Or tm_month = 10 Or tm_month = 12 Then
            MsgBox "Long month : 31 days"
            .Range("D4").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=lmonth"
        Else
            .Range("D4").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=smonth"
        End If
        
        'display
        .Range("B4") = Format(tomorrow, "yyyy")
        .Range("C4") = UCase(Format(tomorrow, "mmmm"))
        .Range("D4") = Format(tomorrow, "dd")
        .Range("B5") = Format(tomorrow, "ddd, mmmm dd, yyyy")
        .Range("C4:D4").Locked = False
        .Protect
    End With
    Stop
End Sub

My variables and named ranges are declared in the "declarations" module
Rich (BB code):
Public wb_SOP2020 As Workbook
Public ws_gui As Worksheet
Public ws_opvals As Worksheet
Public ws_master As Worksheet

Public usr_str_month As String
Public usr_lng_month As Long
Public usr_lng_day As Long
Public usr_str_day As String
Public usr_lng_year As Double
Public usr_lp_yr As Boolean
Public usr_srl_date As Date
Public usr_rng_day As Range
Public rng_smnth As Range 'short month (30 days)
Public rng_lmnth As Range 'long month (31 days)
Public rng_lyfeb As Range 'leap year (Feb = 29 days)
Public rng_nlyfeb As Range 'non leap year (Feb = 28 days)

Sub declarations()
    Stop
    Set wb_SOP2020 = Workbooks("WSOP2020.xlsm")
    Set ws_gui = wb_SOP2020.Worksheets("GUI")
    Set ws_master = wb_SOP2020.Worksheets("MASTER")
    Set ws_opvals = wb_SOP2020.Worksheets("OPVALS")
    Set rng_smnth = ws_opvals.Range("B2:B31")
    Set rng_lmnth = ws_opvals.Range("B2:B32")
    Set rng_lyfeb = ws_opvals.Range("B2:B30")
    Set rng_nlyfeb = ws_opvals.Range("B2:B29")
    
    'Set MyNamedRng = sht.Range("A2:A" & lrow)
    With wb_SOP2020
        .Names.Add Name:="smonth", RefersTo:=rng_smnth
        .Names.Add Name:="lmonth", RefersTo:=rng_lmnth
        .Names.Add Name:="lyfeb", RefersTo:=rng_lyfeb
        .Names.Add Name:="nlyfeb", RefersTo:=rng_nlyfeb
    End With
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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