VBA loop - dates plus 1

relahni

New Member
Joined
Sep 8, 2022
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I'm very new to VBA coding and have relied on macro recording for the code below.

There are three dates in at workbook called "DATES" located in Cells B1:D1 - the code copies those dates and pastes them into a file called "DATA" in cells EU1:EW1

The file called DATA crunches the numbers and pastes to the last available row in a file called "RESULTS"

What I want to do is loop this process - but - add a day to the cells in B1:D1.

For example, if B1 = 25/10/2021 and C1 = 25/10/2020 and D1 = 24/10/2021

I want the next loop to be B1 = 26/10/2021 and C1 to be 26/10/2020 and D1 = 25/10/2021

Then it takes these dates, sends them to get crunched in DATA file then it pastes the data returned into the RESULTS file.

And it repeats this process on and on.

As a work around I am simply copying and pasting - the code below but changing the range to the row below (where I have inputted the dates required).

However, as a VBA rookie, I am interested in learning how I can loop this process to add a day on to the initial dates and repeat this process. All responses very gratefully recieved.

Code below

Range("B1:D1").Select
Selection.Copy
Windows("DATA).xlsx").Activate

Range("EU1:EW1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Range("A755:EK755").Select
Range(Selection, Selection.End(xlUp)).Select
Application.CutCopyMode = False
Selection.Copy
Application.WindowState = xlNormal
Windows("Results.xlsx").Activate
ActiveWindow.SmallScroll Down:=119
Range("A1").End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATES.xlsm").Activate
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to the Board!

You can get and store your date in a variable, like this:
VBA Code:
Dim dt1 as Date
dt1 = Range("B1").Value

Then, let's say that you wanted to loop 10 times through, adding 1 each time.
You could do that like this (this example simply shows the value of the date, and then adds one each time):
VBA Code:
Dim i as Long
For i = 1 to 10
    MsgBox "Current date value is: " & dt1
    dt1 = dt1 + 1
Next i

Hopefully, this shows you how to do what you want.
If you have more questions about it, please let us know.
 
Upvote 0
Welcome to the Board!

You can get and store your date in a variable, like this:
VBA Code:
Dim dt1 as Date
dt1 = Range("B1").Value

Then, let's say that you wanted to loop 10 times through, adding 1 each time.
You could do that like this (this example simply shows the value of the date, and then adds one each time):
VBA Code:
Dim i as Long
For i = 1 to 10
    MsgBox "Current date value is: " & dt1
    dt1 = dt1 + 1
Next i

Hopefully, this shows you how to do what you want.
If you have more questions about it, please let us know.
Thanks Joel.

That is very helpful, but how can I encorporate that into my code?
 
Upvote 0
Thanks Joel.

That is very helpful, but how can I encorporate that into my code?
Not sure who "Joel" is, but you would need to provide us with a little more information, such as, what controls how many times we should loop?
 
Upvote 0
Not sure who "Joel" is, but you would need to provide us with a little more information, such as, what controls how many times we should loop?
So sorry, Joe! I have recently had my eyes tested as well. Apologies.

The number of times it loops could be in a cell - A1 in the DATES file. Thanks again for the response and thanks very much for the warm welcome. I find VBA codes fascinating but as a newbie, I am a bit in the dark.
 
Upvote 0
The number of times it loops could be in a cell - A1 in the DATES file. Thanks again for the response and thanks very much for the warm welcome. I find VBA codes fascinating but as a newbie, I am a bit in the dark.
You could capture that number in a variable, like this:
VBA Code:
Dim n as Long
Windows("DATA).xlsx").Activate
n = Range("A1").Value
You can then loop using that variable later in your code, i.e.
VBA Code:
Dim i as Long
For i = 1 to n
    MsgBox "Current date value is: " & dt1
    dt1 = dt1 + 1
Next i
(This just shows the progression of dates as the loop changes, for demonstration purposes. Obviously, you will want to replace the MsgBox with your code).
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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