VBA date loop of existing VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I currently have a date loop that is working as intended, I am unfortunately going to have to modify it to accommodate being given 2 dates then require each date being looped. for example:

10/1/2021
9/28/2021

Would need 9/28, 9/29, 9/30 to loop and no longer will have it conveniently listed for me. is there a way to modify the below to accommodate? currently all the dates are listed so the below works but it will no longer be the case so any help is very much appreciated.

VBA Code:
Sub RUN_DATE_LOOP()
Dim Dates As Date
Dim i As Long

' Run Dates loop
i = 1
Do Until Sheets("Date Loop").Range("Dates").Offset(i, 0) = ""
Dates = Sheets("Date Loop").Range("Dates").Offset(i, 0)
Sheets("Dist").Range("SP") = Dates
Call SECDIST
i = i + 1
Loop

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I do not understand your question, and I think others may be in the same situation (judging by the lack of responses).
Can you walk us through an actual example of your data, and show us what you want to happen?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Apologies for the not so clear explanation. Let me try again.

Currently i have a sheet that lists my dates for my date loop to run:
09/30/21
09/29/21
09/28/21

I will have a sheet where Ill only be given a START and a END like so

B4 = 10/01/21 (end)
B5 = 09/28/21 (start)

So what I am looking to do is run a date loop like I use to have just ill be given 2 dates where everything between are the dates it needs to loop for. so 09/28 - 9/30 not including 10/1
 
Upvote 0
Sorry, that doesn't really help me. Remember, we have no background on your project - you know what your data looks like and what you want it to do, but we do not.
I am very confused. You seem to have two loops (not sure)?
What exactly is each supposed to be doing?

Please give us exact names of sheets and ranges, and walk us through an actual example step-by-step, showing us exactly what your expected result is.
 
Upvote 0
I am sorry i thought I did in my original? so let me try to clarify again summarizing everything

Current VBA:
VBA Code:
VBA Code:
Sub RUN_DATE_LOOP()
Dim Dates As Date
Dim i As Long

' Run Dates loop
i = 1
Do Until Sheets("Date Loop").Range("Dates").Offset(i, 0) = ""
Dates = Sheets("Date Loop").Range("Dates").Offset(i, 0)
Sheets("Dist").Range("SP") = Dates
Call SECDIST
i = i + 1
Loop

End Sub
  1. Currently have 1 date loop (see above)
  2. Sheet = DATES (dates are manually added/listed) A2-A4
  3. Sheet = DIST (where i run date loop)
  4. Dates used to run SECDIST; then loops for each day doing same thing, new day secdist, etc.
  5. There will be a New Sheet = START
    1. In said sheet is two dates
      1. B4 = 10/01/21 (end)
      2. B5 = 09/28/21 (start)
  6. New sheet is replacing #2 above therefore resulting in #4 above to no longer work
in simple terms I am going from a listed range manually inputted to being provided 2 dates where everything between two dates i need to loop like my prior VBA did when it was listed. Sorry if it is causing confusion I am not sure how else to explain this in better layman's terms.
 
Upvote 0
OK, I think I see now. You are trying to replace the current loop with the new structure.
Here is a simple little example that shows you how to do that:
VBA Code:
    Dim sDate As Date
    Dim eDate As Date
    Dim d As Date

'   Set starting and ending dates
    sDate = Sheets("Start").Range("B5")
    eDate = Sheets("Start").Range("B4")

'   If end date is after start date, loop through dates
    If eDate >= sDate Then
        For d = sDate To eDate
            MsgBox d
        Next d
    End If
 
Upvote 0
Solution
OK, I think I see now. You are trying to replace the current loop with the new structure.
Here is a simple little example that shows you how to do that:
VBA Code:
    Dim sDate As Date
    Dim eDate As Date
    Dim d As Date

'   Set starting and ending dates
    sDate = Sheets("Start").Range("B5")
    eDate = Sheets("Start").Range("B4")

'   If end date is after start date, loop through dates
    If eDate >= sDate Then
        For d = sDate To eDate
            MsgBox d
        Next d
    End If
Awesome I will test this out and let you know. Appreciate the help and apologies on the vagueness on my prior posts.
 
Upvote 0
Where should I place setting the date to my other sheet? for example like my old vba

Sheets("Dist").Range("SP") = Dates
Call SECDIST
i = i + 1
Loop
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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