xyz
xyz

Reputation: 2300

Add x number of days to a date with vba in excel

I am tring to add x number of days to a Long date with a pop up box.

Public Function AskForDeadlinePlus4() As String
    Dim strUserResponse As String

    strUserResponse = InputBox("Enter Validuntil Date: Add # of Days To Survey end date")
    strUserResponse = FormatDateTime(strUserResponse + I2, vbLongDate)
    ActiveSheet.Cells(2, 10).Value = strUserResponse 'the 2, 10 is the cell reference for J2 - row 2, column 10.

End Function

Where Survey end date in cell I2.

When I run this I get (Googling how to do this I am tiring)

4 + I2 (where I2 = Friday, April 05, 2013) >> Wednesday, January 03, 1900

of course I need Tuesday, April 09, 2013

Thanks

Upvotes: 7

Views: 62260

Answers (3)

Volkan Yurtseven
Volkan Yurtseven

Reputation: 444

Instead of using DateAdd, which requires more typing, you could also use DateValue. Following would do it.

DateValue(strUserResponse )+I2

Another solution would be using the conversion function, CDate.

CDate(strUserResponse )+I2

Upvotes: 0

glh
glh

Reputation: 4972

I think this code is what your after using the DateAdd(<base e.g. Day = "D">, <number>, <date>) function:

Public Function AskForDeadlinePlus4() As String
    Dim strUserResponse As Date, iNumber As Long, rResponse As Variant

    AskForDeadlinePlus4 = "" 'set default value
    iNumber = CLng([I2])
    rResponse = InputBox("Enter Validuntil Date: Add " & iNumber & " Day(s) To Survey end date")

    If rResponse = False Then
        'no value entered
        Exit Function            
    ElseIf Not IsDate(rResponse) Then
        'no date entered
        Exit Function
    Else
        'valid date entered
        strUserResponse = DateAdd("D", iNumber, CDate(rResponse))
    End If

    AskForDeadlinePlus4 = FormatDateTime(strUserResponse, vbLongDate)    
End Function

Just a few points though:

  • The input function will return the Boolean FALSE if no input is entered.
  • The test you used above is a function and will return a value when used
  • If you want to use in in another VBA code, i = AskForDeadlinePlus4 is its usage;
  • But you can also use it in a cell but only when necessary as with every calculation this will prompt an input and for every cell its in, =AskForDeadlinePlus4; and
  • Plus I've added a check to see if a date was entered as the user may not enter a valid one.

If you want to use in VBA:

Sub GetInfo()
    'the 2, 10 is the cell reference for J2 - row 2, column 10.
    ActiveSheet.Cells(2, 10).Value = AskForDeadlinePlus4
End Sub

Upvotes: 2

David Zemens
David Zemens

Reputation: 53623

Have you used the DateAdd function?

Sub DateExample()

Dim strUserResponse As String '## capture the user input'
Dim myDate As Date     '## the date you want to add to'
Dim numDays As Double  '## The number of days you want to add'


strUserResponse = InputBox("Enter Validuntil Date: Add # of Days To Survey end date")
numDays = InputBox("How many days to add?")
myDate = CDate(strUserResponse)

MsgBox DateAdd("d", numDays, myDate)


End Sub

Upvotes: 17

Related Questions