1

I keep getting a syntax error as well as an identifying error at the IF statement.

Function Remove()

     Dim DateDue As Date
     Dim i As Long
     DateDue = Date

     For i = 2 To Sheets("MasterSheet").Cells(Rows.Count, 1).End(xlUp).Row

        If DateDue = DateSerial(Year(Sheets("MasterSheet").Cells(i, 1)), 
                     Month(Sheets("MasterSheet").Cells(i, 1).Value),
                     Day(Sheets("Mastersheet").Cells(i, 1).Value)) Then
        msgbox(dnuanfdqduii)
        End If

     Next i

End Function
2
  • 2
    Your lines have next line inserted. Make sure you have that formula all on one line. Paste this in: If DateDue = DateSerial(Year(Sheets("MasterSheet").Cells(i, 1)), Month(Sheets("MasterSheet").Cells(i, 1).Value), Day(Sheets("Mastersheet").Cells(i, 1).Value)) Then Commented Jul 23, 2021 at 6:24
  • 3
    If you want to break a line of code to multiple lines, you need to add a space then underscore at the end of each line (except the last). Read this
    – Raymond Wu
    Commented Jul 23, 2021 at 6:26

1 Answer 1

1

VBA is sensitive to line breaks when you write a logical construction (like If ... Then ... End If in your case).

If you need to break a one line statement to two and more lines (for easy reading, for example), you need to use the VBA's Line-Continuation character _ for saving logical sense. It is reserved underscore character to continue a statement from one line to next.

So, your code:

If DateDue = DateSerial(Year(Sheets("MasterSheet").Cells(i, 1)),
            Month(Sheets("MasterSheet").Cells(i, 1).Value),
            Day(Sheets("MasterSheet").Cells(i, 1).Value)) Then

End If

can be corrected as follows:

If DateDue = DateSerial(Year(Sheets("MasterSheet").Cells(i, 1)), _
                        Month(Sheets("MasterSheet").Cells(i, 1).Value), _
                        Day(Sheets("MasterSheet").Cells(i, 1).Value)) Then

End If

You can read more about this at the link: how to break and combine statements in visual basic сode

Not the answer you're looking for? Browse other questions tagged or ask your own question.