Well, I tested this code and it works nice:
Sub Test()
Dim mon As Integer
'Check Whether the Start Date Text Box is Empty or Not
If IsNull(Cells(4, 3).Text) = True Then
Cells(5, 3).Value = ""
Else
Cells(4, 4).Value = DateAdd("yyyy", 1, Cells(4, 3).Value - 1)
Cells(4, 4).NumberFormat = "d-mmm-yyyy"
mon = DateDiff("m", Cells(4, 3), Cells(4, 4) + 1)
Cells(5, 3).Value = mon
End If
End Sub
When Format function was used, I couldn't just add 1 day in DateDiff. It is because you can't add 1 to a string, I guess.
Sub Test2()
Dim xDate As Long
Dim xNewDate As Long
xDate = 40179 'It is 01.01.2010
xNewDate = DateAdd("yyyy", 1, xDate - 1)
xDateFormat = Format(xDate, "dd-mmm-yyyy")
xNewDateFormat = Format(xNewDate, "dd-mmm-yyyy")
MsgBox DateDiff("m", xDate, xNewDate + 1)
Debug.Print xDate
Debug.Print Format(xDate, "dd-mmm-yyyy")
Debug.Print xNewDate
Debug.Print Format(xNewDate, "dd-mmm-yyyy")
Debug.Print ""
'Debug.Print CLng(xDateFormat) 'Type Mismatch
'Debug.Print CLng(xNewDateFormat) 'Type Mismatch
Debug.Print DateDiff("m", xDateFormat, xNewDateFormat) 'It works so DateDiff converts it
Debug.Print DateDiff("m", DateDiff("d", 0, xDateFormat), DateDiff("d", 0, xNewDateFormat) + 1) 'Now you can add one day
End Sub
I hope that this code should work:
Dim mon As Integer
'Check Whether the Start Date Text Box is Empty or Not
If IsNull(Me.TextBox1.Text) = True Then
Me.TextBox3.Value = ""
Else
Me.TextBox2.Value = Format(DateAdd("yyyy", 1, Me.TextBox1.Value) - 1, "dd-mmm-yyyy")
mon = DateDiff("m", Me.TextBox1, Me.TextBox2)
mon = DateDiff("m", DateDiff("d", 0, Me.TextBox1), DateDiff("d", 0, Me.TextBox2) + 1)
Me.TextBox3.Value = mon
End If
TextBox2
? 01-Oct - 1 will be 30-Sep, and there are only 11 months from Oct-2018 to Sep-2019. If you do need the- 1
, then add it back inDateDiff
. – newacc2240TextBox2
is not from the user, why don't you just assign12
toTextBox3
:P – newacc2240