new11
new11

Reputation: 55

Get number of days between two dates using alternate day as a variable

I have an excel userform that contains four text boxes: start date, end date, Alternate Day number & result.

A user enters a starting and ending date. For example Start Date: 01/oct/2020 and End Date: 04/jan/2021.

In the Alternate Day number text box, the user enters an alternate day number, say 4. This represents that an action begins on every 4th day.

How could I code it so that VBA can work out the total number of alternate days between the two dates?

Upvotes: 0

Views: 238

Answers (2)

Pᴇʜ
Pᴇʜ

Reputation: 57683

Basically your question is not clear to give only one answer. Let's assume the following 2 dates:

enter image description here

You can get the total number as The Guru answered as

=(end_date - start_date)/alternate_day_number

this might come up with something like 3.5 if you only want full days as total number you need to take the integer

=INT(end_date - start_date)/alternate_day_number)

So for this example above the result is 3 but as you can see in the colored numbers depending wether you consider both start_date and end_date to be in the total number you need to add +1 to the result (to make it all 4 dates).

Upvotes: 0

The Guru
The Guru

Reputation: 115

You could even do this within Excel without VBA if you want to take that route. You could simply use the formula

= (end_date - start_date)/alternate_day_number

You could of course program the same formula in VBA as well.

Upvotes: 2

Related Questions