Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

excel

vba

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

like image 714
xyz Avatar asked Apr 28 '13 03:04

xyz


People also ask

How do you add days to a date in VBA Excel?

Use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now. To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").


2 Answers

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
like image 132
David Zemens Avatar answered Oct 01 '22 03:10

David Zemens


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
like image 21
glh Avatar answered Oct 01 '22 01:10

glh