date - 在Excel中使用vba向日期添加x天数

标签 date excel vba

我试图通过弹出框向长日期添加 x 天数。

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

调查结束日期位于单元格 I2 中。

当我运行这个时,我得到(谷歌搜索如何做到这一点我很累)

4 + I2(其中 I2 = 2013 年 4 月 5 日星期五)>> 1900 年 1 月 3 日星期三

当然我需要2013年4月9日星期二

谢谢

最佳答案

您使用过DateAdd函数吗?

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

关于date - 在Excel中使用vba向日期添加x天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259166/

相关文章:

vba - 自动在小数点分隔符后写入 2 位数字

xml - CalDav Propfind 响应中的日期格式 - 如何更改它?

java - 使用 simpleDateFormat 将字符串转换为 Util Date 不起作用

C#——高效写入Excel文件内容

Excel VBA - 索引和匹配 VBA 错误

vba - 设备字体 cpi 选项仅显示在 Crystal 报表字体属性中

r - 间隔 R 内的周末日期

sql - 如何获取范围内一年中特定月份的天数总和

vba - Excel VBA反向for循环步进超出最终值

excel - 执行Word邮件合并