VBA/Macro Copy N Times N=Days in Dateformat

tobi97

New Member
Joined
Jul 23, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,
Im new to using excel behind like some standard stuff.
So i have a cell for a date range (green marked).
Now i want the rows 7 to 27 copied underneath N times.
N = the days in the date range.
Example:
15.8.2023 - 20.8.2023 = 6 days
So rows 7 to 27 copied 6 times underneath.

As little special but not so much important:
In every copy the Date (blue marked) should change to the according day in the date range.
 

Attachments

  • Bildschirmfoto 2023-07-23 um 10.44.10-min.png
    Bildschirmfoto 2023-07-23 um 10.44.10-min.png
    252.2 KB · Views: 4

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Welcome to the MrExcel board!

For the future , I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

You have not said exactly where to paste the copies but give this a try with a copy of your workbook.
I have assumed what looks like merged cells E7:U7 have an appropriate date format applied.

VBA Code:
Sub Copy_Range_N_Times()
  Dim DateStartEnd As Variant
  Dim i As Long
  
  DateStartEnd = Split(Range("B4").Value, "-")
  Application.ScreenUpdating = False
  For i = 1 To DateValue(DateStartEnd(1)) - DateValue(DateStartEnd(0)) + 1
    Range("B7:V27").Copy Destination:=Range("B" & 7 + 22 * i)
    Range("E" & 7 + 22 * i).Value = DateValue(DateStartEnd(0)) + i - 1
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Welcome to the MrExcel board!

For the future , I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

You have not said exactly where to paste the copies but give this a try with a copy of your workbook.
I have assumed what looks like merged cells E7:U7 have an appropriate date format applied.

VBA Code:
Sub Copy_Range_N_Times()
  Dim DateStartEnd As Variant
  Dim i As Long
 
  DateStartEnd = Split(Range("B4").Value, "-")
  Application.ScreenUpdating = False
  For i = 1 To DateValue(DateStartEnd(1)) - DateValue(DateStartEnd(0)) + 1
    Range("B7:V27").Copy Destination:=Range("B" & 7 + 22 * i)
    Range("E" & 7 + 22 * i).Value = DateValue(DateStartEnd(0)) + i - 1
  Next i
  Application.ScreenUpdating = True
End Sub
Hi!
Works perfect
Thanks!!
I just have small follow up question.
If i want it to go/copy it to the right side of it not underneath which value do i need to change in your code?
 
Upvote 0
If i want it to go/copy it to the right side of it not underneath which value do i need to change in your code?

Rich (BB code):
Sub Copy_Range_N_Times_v2()
  Dim DateStartEnd As Variant
  Dim i As Long
  
  DateStartEnd = Split(Range("B4").Value, "-")
  Application.ScreenUpdating = False
  For i = 1 To DateValue(DateStartEnd(1)) - DateValue(DateStartEnd(0)) + 1
    Range("B7:V27").Copy Destination:=Cells(7, 2 + 22 * i)
    Cells(7, 5 + 22 * i).Value = DateValue(DateStartEnd(0)) + i - 1
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,198
Latest member
MhammadishaqKhan

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