Home » excel » excel – Compile error: Expected: =

excel – Compile error: Expected: =

Posted by: admin May 14, 2020 Leave a comment

Questions:

I got the error when implementing Sub TabPaint within Sub DateDiff

Public Sub TabPaint(ss As Integer, cc As Integer)
  With Sheets(ss).Tab
      .Color = cc
      .TintAndShade = 0
  End With
End Sub


Public Sub DateDiff(date1 As String, date2 As String, shn As Integer)
    If DateDiff("d", date1, date2, vbMonday, vbFirstJan1) < 0 Then
        TabPaint (shn, 255)
    Else
        TabPaint(shn,5287936)
    End If
End Sub
How to&Answers:

In VBA, if you aren’t assigning the return value of a function to anything, or if the method is a SubRoutine, you leave out the parenthesis, so try this:

Public Sub TabPaint(ss As Integer, cc As Integer)
  With Sheets(ss).Tab
      .Color = cc
      .TintAndShade = 0
  End With
End Sub


Public Sub DateDiff(date1 As String, date2 As String, shn As Integer)
    If DateDiff("d", date1, date2, vbMonday, vbFirstJan1) < 0 Then
        TabPaint shn, 255
    Else
        TabPaint shn, 5287936
    End If
End Sub

Answer:

when you call subs like your sub TabPaint you must not use brackets so try this:

TabPaint shn, 255

instead of this:

TabPaint(shn, 255)