Reputation: 223
I checked here and all over the web and found a few solutions to this that I tried, including casting Now
as a CDate
, but nothing works.
Using the VBA Editor w/ Excel 2010 on Win7.
Even the example code from the help section is throwing this error and the date used in the Month()
function is explicitely initialized in numeric form.
In both cases I tried using CDate()
on the month()
argument, it does not work. In my original code I also tried using Date
instead of Now
, no effect either.
Here is my original code, which casts the error on the If condition:
Function SetNextTaskNb()
Dim seqNb As String
seqNb = ThisWorkbook.Worksheets("Persistent").Range("A" & 1).Value
Dim Nbs() As String
Nbs = Split(seqNb, ".", 2)
Dim month, currentNb, nextNb As Integer
month = CInt(Nbs(0))
currentNb = CInt(Nbs(1))
If month(Now) = month Then
nextNb = currentNb + 1
Else
nextNb = 1
End If
ThisWorkbook.Worksheets("Persistent").Range("A" & 1).Value = currentMonth + "." + nextNb
ThisWorkbook.Worksheets("Sheet1").Range("A" & 1).Value = currentMonth + "." + nextNb
End Function
Here is the example code from the VBA Editor Help section, which, copy-pasted with no modifications throws the same error on the first Debug.Print
. It won't even display it.
Dim MyDate, MyMonth
MyDate = #2/12/1969# ' Assign a date.
Debug.Print "month is " & month(MyDate)
Debug.Print "rgMonth.Cells(i, j).Value is " & rgMonth.Cells(i, j).Value
MyMonth = month(CDate(MyDate)) ' MyMonth contains 2.
I know I'm supposed to give the Month() function a date in numeric form, and that it returns an Integer. I tried everything I could think of.
Here is proof this is the code from the Help:
Upvotes: 1
Views: 1061
Reputation: 96771
you are using Month as both the name of a function and the name of a variable................give the variable a different name.
Upvotes: 7