Object required error

Object required error

Hi,

I am getting an 'Object required' error on the line

Call HideColumnsOutsideRange(ws, startOfWeek, endOfWeek) 

when I run the ShowCurrentWeek macro but not when I run the ShowCurrentMonth macro. 

Any ideas?

Regards,
GW

Option Explicit

Sub HideColumnsOutsideRange(ws as Worksheet, startDate As Date, endDate As Date)
    Dim lastCol As Long
    Dim c As Long
    Dim hdr As Variant
    Dim dt As Date

    ' Find last used column in row 1
    Application.ScreenUpdating = False

    ws.Cells.EntireColumn.Hidden = False
    
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    For c = 1 To lastCol
        hdr = ws.Cells(1, c).Value

        If IsDate(hdr) Then
            ' Normalize to pure date (drop time / timezone artifacts)
            dt = DateSerial(Year(CDate(hdr)), Month(CDate(hdr)), Day(CDate(hdr)))
            ' Debug.print "dt: " & dt & " start: " & startDate & " end: " & endDate
            ' Hide if outside the given range
            If (dt < startDate) Or (dt > endDate) Then
                ws.Columns(c).Hidden = True
            End If
        Else
            ws.Columns(c).Hidden = True
        End If
    Next c

    Application.ScreenUpdating = True
End Sub

Sub ShowCurrentMonth()
    Dim ws As Worksheet
    Dim today As Date, startOfMonth As Date, endOfMonth As Date

    Set ws = ActiveSheet
    today = DateValue(Now)

    startOfMonth = DateSerial(Year(today), Month(today), 1)
    endOfMonth   = DateSerial(Year(today), Month(today) + 1, 0)

    Call HideColumnsOutsideRange(ws, startOfMonth, endOfMonth)
End Sub

Sub ShowCurrentWeek()
    Dim ws As Worksheet
    Dim today As Date, startOfWeek As Date, endOfWeek As Date

    Set ws = ActiveSheet
    today = DateValue(Now)

    startOfWeek = today - (Weekday(today) - 1)  ' Sunday–Saturday
    'Debug.Print "startOfWeek: " & startOfWeek
    endOfWeek = startOfWeek + 6

    Call HideColumnsOutsideRange(ws, startOfWeek, endOfWeek)
End Sub

    Access your files securely from anywhere







                            Zoho Developer Community




                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                  • Ask the Experts





                                                            Manage your brands on social media



                                                                  Zoho TeamInbox Resources



                                                                      Zoho CRM Plus Resources

                                                                        Zoho Books Resources


                                                                          Zoho Subscriptions Resources

                                                                            Zoho Projects Resources


                                                                              Zoho Sprints Resources


                                                                                Qntrl Resources


                                                                                  Zoho Creator Resources



                                                                                      Zoho CRM Resources

                                                                                      • CRM Community Learning Series

                                                                                        CRM Community Learning Series


                                                                                      • Kaizen

                                                                                        Kaizen

                                                                                      • Functions

                                                                                        Functions

                                                                                      • Meetups

                                                                                        Meetups

                                                                                      • Kbase

                                                                                        Kbase

                                                                                      • Resources

                                                                                        Resources

                                                                                      • Digest

                                                                                        Digest

                                                                                      • CRM Marketplace

                                                                                        CRM Marketplace

                                                                                      • MVP Corner

                                                                                        MVP Corner









                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now


                                                                                            Zoho Show Resources

                                                                                              Zoho Writer

                                                                                              Get Started. Write Away!

                                                                                              Writer is a powerful online word processor, designed for collaborative work.

                                                                                                Zoho CRM コンテンツ



                                                                                                  Nederlandse Hulpbronnen


                                                                                                      ご検討中の方