473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need VB code for excel to pupulate specific date ranges

Hi fantastic members,

I have created a monstrous calculator for work within my organisation. I have a user inoput screen to get a start and end date so I am pulling variables from there.

What i need is to be able to populate cells starting at A12:B12 with the start date in cell A12, then adding one month and getting that to populate into cell B12.

I need this to continue subsequently until the end date is reached. Managed to find this code which starts and stops on the dates I want but it populates days and only in column A.

Any help would be much appreciated. Just to clarify I would be looking at something like this:

A12 shows "06/10/10"
B12 shows "05/11/10"
A13 shows "06/11/10"
B13 shows "05/12/10" etc etc until the end date is reached

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton21_Click()
  2.  
  3. Dim startdate As Date
  4. Dim enddate As Date
  5. Dim row As Double
  6.  
  7. startdate = Range("D9").Value
  8. enddate = Range("D10").Value
  9. row = 0
  10.  
  11. With Worksheets("Sheet1")
  12. Range("A12:B12").Select
  13. Do Until DateAdd("d", 1, startdate) = enddate + 1
  14.     ActiveCell.Offset(row, 0).Value = DateAdd("d", 1, startdate)
  15.     startdate = startdate + 1
  16.     row = row + 1
  17. Loop
  18. End With
  19. End Sub
  20.  
Mar 5 '15 #1
1 1233
twinnyfo
3,653 Expert Mod 2GB
benny1983,

Could you explain a little bit more about exactly what it is you want? Are you adding one month or adding 30 days? There is a huge difference. If you are adding one month, then change the value in the DateAdd() function. Otherwise, use 30 as the argument. Here is a sample:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton21_Click()
  2.     Dim startdate As Date
  3.     Dim enddate As Date
  4.     Dim row As Double
  5.  
  6.     startdate = Range("D9").Value
  7.     enddate = Range("D10").Value
  8.     row = 12
  9.  
  10.     With Worksheets("Sheet1")
  11.         Do While Not startdate > enddate
  12.             .Cells(row, 1) = startdate
  13.             .Cells(row, 2) = DateAdd("m", 1, startdate) '1 month
  14.             .Cells(row, 2) = DateAdd("d", 30, startdate) '30 days
  15.             startdate = startdate + 1
  16.             row = row + 1
  17.         Loop
  18.     End With
  19. End Sub
Hope this hepps!
Mar 6 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
2
by: junkaccount | last post by:
Hello, Using Access 2000 I would like to create one report that returns sales data for various date ranges input by the user i.e. weekly, monthly, etc. This report is bound to a query that pulls...
6
by: alexanderpope11 | last post by:
Hello, how do I write this SQL: I would like to check the Main table for invalid rows. An invalid row is: any row where the Start_date to stop_date range overlaps an invalid date in the Code...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
1
by: Matt | last post by:
Hi all, I have a database with a table storing a list of names, invoice dates, and invoice amounts. What I'm looking to do is to create a sum of the invouice amounts based on a range of...
10
by: kyosohma | last post by:
Hi, I am working on a timesheet application in which I need to to find the first pay period in a month that is entirely contained in that month to calculate vacation time. Below are some example...
16
by: Alex30093 | last post by:
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work. Situation: I want to use one table of events that...
8
by: xzmilan | last post by:
Totally new to using loop through's but I have a feeling it's what I need. I have two tables tbl1 has a user ID and a change date, a date they updated their information tbl2 has the user ID...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.