how to get the following output(reference image below) using VBA for getting list of dates from a particular date to particular date?

abhi221996

New Member
Joined
Sep 29, 2021
Messages
35
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
HOW CAN I GET THIS OUTPUT USING VBA?
キャプチャ.PNG
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Assums with giving start and end date:
VBA Code:
Sub test()
Dim startD&, endD&, k&
startD = #12/15/2021#
endD = #12/31/2022#
For Each cell In Range("A4").Resize(endD - startD, 1)
k = k + 1
cell.Value = startD + k
Next
End Sub
 
Upvote 0
Hi Abhi,

Use below code:

VBA Code:
Sub showDates()

Dim fromDate As Date, toDate As Date, myDate As Date
Dim rowno As Integer

rowno = 1

fromDate = #12/15/2021#
toDate = #1/10/2022#

myDate = fromDate
Do While myDate <= toDate
    Worksheets("Sheet1").Cells(rowno, 1) = myDate
    myDate = myDate + 1
    rowno = rowno + 1
Loop

End Sub
 
Upvote 0
Thanks all the solutions.
is it possible to do it just by for loop.
 
Upvote 0
Using For Loop:

VBA Code:
Sub showDatesFor()

Dim fromDate As Date, toDate As Date, myDate As Date
Dim rowno As Integer

rowno = 1

fromDate = #12/15/2021#
toDate = #1/10/2022#

For myDate = fromDate To toDate
    Worksheets("Sheet1").Cells(rowno, 1) = myDate
    rowno = rowno + 1
Next

End Sub
 
Upvote 0
Solution
Thanks all the solutions.
is it possible to do it just by for loop.
Hi, "For Each" is one of "loop" but using specific object: "range"
Code:
For Each cell In Range("A4").Resize(endD - startD, 1)
k = k + 1
cell.Value = startD + k
Next
 
Upvote 0
Understood! thanks bebo021999
I just wanted to know how to do it by "for" loop.
 
Upvote 0
Hello all!
I have learned a new function called Dateadd how can i do it by that please suggest?
 
Upvote 0
Hello Abhi,

Below code we can use if would like to use DateAdd method.

VBA Code:
Sub showDates()

Dim fromDate As String, toDate As String
Dim rowno As Integer

fromDate = "12/15/2021"
toDate = "10/1/2022"

Worksheets("Sheet2").Cells(1, 2) = fromDate

For rowno = 1 To DateDiff("d", fromDate, toDate)
    Worksheets("Sheet2").Cells(rowno + 1, 2) = DateAdd("d", 1, Sheets("Sheet2").Cells(rowno, 2))
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,128
Members
449,206
Latest member
burgsrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top