Date 使用excel中的vba将x天数添加到日期
我想用一个弹出框给一个长日期加上x个天数Date 使用excel中的vba将x天数添加到日期,date,excel,vba,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 + I
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
我想这段代码就是您在使用
DateAdd(,)
函数后的代码:
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
不过有几点:
- 如果未输入任何输入,输入函数将返回布尔值FALSE
- 上面使用的测试是一个函数,使用时将返回一个值
- 如果要在另一个VBA代码中使用,
是它的用法李>i=AskForDeadlinePlus4
- 但您也可以在单元格中使用它,但只有在必要时,与每次计算一样,这将提示输入,并且对于其所在的每个单元格,
;及=AskForDeadlinePlus4
- 此外,我还添加了一个检查,以查看是否输入了日期,因为用户可能输入的日期无效
Sub GetInfo()
'the 2, 10 is the cell reference for J2 - row 2, column 10.
ActiveSheet.Cells(2, 10).Value = AskForDeadlinePlus4
End Sub
您也可以使用DateValue,而不是使用DateAdd,后者需要更多的键入。下面就可以了
DateValue(strUserResponse )+I2
另一种解决方案是使用转换函数CDate
CDate(strUserResponse )+I2
strUserResponse=FormatDateTime(DateAdd(d,strUserResponse,Range(“I2”)),vbLongDate)
-你好,Tim,我尝试了你的建议,我得到了错误“无效的过程调用或参数”不是舒尔如何解决我在另一个子集末尾这样调用的问题,调用askFordLeadlinePlus4,如果我更改DateAdd,它在其原始版本中也会起作用(d,strUserResponse,Range(“I2”))
到日期添加(“d”,strUserResponse,Range(“I2”))
然后我得到一个日期输出“2012年4月4日星期三”+4>“1900年1月3日星期三”这样做不是更好吗,因为strUserResponse是单元格I2中包含的日期的天数?日期存储为long
值,因此从技术上讲,像1.5
这样输入的值不会导致错误,但任何像这样的非数值都会导致错误
将导致不匹配
错误。对于健壮的解决方案,是的,最终需要添加一些逻辑以确保用户传递允许的输入,但这不是您要问的问题:)你好,David,这里有两个Tim,我是发布问题的人。我如何将输出添加到单元格J2我尝试了ActiveSheet.Cells(2,10)。Value=myDate
,但我得到的是原始日期,没有添加天数。我想使用另一个Tim的建议(因为这就是我想说的)但这也不起作用。如果我添加myDate=CDate(“d”,CInt(strUserResponse,Range(“I2”))
它会变红并说“预期编译错误:)”,这要归功于两个属性ActiveSheet.Cells(2,10)。Value=DateAdd(“d”,numDays,myDate)
谢谢David,我使用了:ActiveSheet.Cells(2,10)。Value=FormatDateTime(DateAdd(“d”,numDays,myDate),vbLongDate)你好,glh,谢谢你的代码,当我运行它时,我得到了错误“类型不匹配”,不知道如何修复它。感谢它一开始运行