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
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
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.
The error is raised because the Match function fails to find a match.
You can use error trapping to catch no finds.
Dates are really tricking things to find. You may need to convert the date to a number.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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks