我有一种使用用户表格输入开始日期和结束日期的方法。如何使用VBA
在此开始日期和结束日期之间输入日期并将其作为输入提供给excel工作表?
例如,如果我的开始日期 1st jan 2012
和我的结束日期是1st april 2012
,我想在{{ 1}}的形式:
excel sheet
请给我建议一个VBA代码以完成相同的操作。
答案 0 :(得分:0)
很难理解您的意思,但是您可以尝试以下方法:
Sub test3()
Dim Startdate As Date, EndDate As Date, AdditionalDate As String
Dim DatesDiff As Integer
Dim i As Integer
Dim LastRowA As Long
Startdate = Format(CDate("01/01/12"), "DD/MM/YYYY")
EndDate = Format(CDate("01/04/12"), "DD/MM/YYYY")
If Sheet1.Range("A1").Value = "" Then
Sheet1.Range("A1").Value = Startdate
Else
LastRowA = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Sheet1.Range("A" & LastRowA + 1).Value = Startdate
End If
DatesDiff = Month(EndDate) - Month(Startdate) - 1
For i = 1 To DatesDiff
AdditionalDate = Left(CStr(Startdate), 2) & "/" & (Month(CStr(Startdate)) + i) & "/" & Right(CStr(Startdate), 4)
LastRowA = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Sheet1.Range("A" & LastRowA + 1).Value = CDate(AdditionalDate) 'Format(CDate(AdditionalDate), "DD/MM/YYYY")
Next i
LastRowA = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Sheet1.Range("A" & LastRowA + 1).Value = EndDate
End Sub