+ Reply to Thread
Results 1 to 4 of 4

Unable to Match property of Worksheet class function

Hybrid View

  1. #1
    Registered User
    Join Date
    09-17-2009
    Location
    Russia, St. Petersburgh
    MS-Off Ver
    Excel 2003/2007
    Posts
    62

    Unable to Match property of Worksheet class function

    Hi in below stated code i do get this error:
    Unable to Match property of Worksheet class function.
    It seems I made everything right, but I cannot figure out why this message appears. Hope you can find out the problem...

    Private Sub StudentButton_Click()
        Dim DateField As Range, ResultTable As Range
        Dim StartDay As Date, EndDay As Date
        Dim lRow As Long, lColumn As Long
        Dim sDayPos As Long, eDayPos As Long
        Dim StartRow As Integer, StartColumn As Integer
        Dim MyMonth As Integer, MyYears As Integer, lDate As Integer
    
        StartRow = Range("FirstCell").Row
        StartColumn = Range("FirstCell").Column
    
        Set ResultTable = Range("ResultTable")
        Set DateField = Sheets(2).Range("Dates")
        
        'Getting Last column number
        With Range(StartRow & ":" & StartRow)
             lColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        End With
    
        For MyYears = 1 To lColumn - StartColumn
            For MyMonth = 1 To 12
    
    ----------------------------------------- SOME PART OF CODE BETWEEN ------------------------------
    
                'Getting positions for Fist day and last day of a month in Datafield Range
                With WorksheetFunction
                    sDayPos = .Match(StartDay, DateField, 0)   <<<<<<< CAUSES ERROR
                    eDayPos = .Match(EndDay, DateField, 0)    <<<<<<< CAUSES ERROR
                End With
    
            MsgBox sDayPos & " and " & eDayPos
            Next MyMonth
        Next MyYears
    End Sub

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Unable to Match property of Worksheet class function

    hi bambino_32
    Try this....
    with Application.WorksheetFunction
                    sDayPos = .Match(StartDay, DateField, 0)   
                    eDayPos = .Match(EndDay, DateField, 0)    
     End With
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Unable to Match property of Worksheet class function

    The error is raised because the Match function fails to find a match.

    You can use error trapping to catch no finds.
        On Error GoTo ErrMatch
        
        With Application.WorksheetFunction
            sDayPos = .Match(StartDay, DateField, 0)
        End With
        
        Exit Sub
    
    ErrMatch:
        MsgBox "No Match"
        Exit Sub
        
    End Sub
    Dates are really tricking things to find. You may need to convert the date to a number.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: Unable to Match property of Worksheet class function

    I assume there is some code missing that actually assigns a value to startday, so try this:
    .Match(CLng(StartDay), DateField, 0)
    Remember what the dormouse said
    Feed your head

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1