+ Reply to Thread
Results 1 to 4 of 4
  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...

    Code:
    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 Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Unable to Match property of Worksheet class function

    hi bambino_32
    Try this....
    Code:
    with Application.WorksheetFunction
                    sDayPos = .Match(StartDay, DateField, 0)   
                    eDayPos = .Match(EndDay, DateField, 0)    
     End With
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  3. #3
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    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:
    Code:
    .Match(CLng(StartDay), DateField, 0)
    So long, and thanks for all the fish.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    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.
    Code:
        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

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.2.0