VBA - Calculate interval between two dates

OldAndWeak182

New Member
Joined
Apr 29, 2021
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi guys,
Would you be kind enough to help me out? I hit my head all day but finally gave up.
So...

first example:
In A1 I have 01/01/2021 and in A2 I have 05/01/2021 (european date).
In VBA how can I do to calculate the range "in between" and write for example in B1 01/01/2021, in B2 02/01/2021, in B3 03/01/2021 etc?

second example linked to the first:
Same scenario as before.
This time, however, I would like to insert variables: from B1 you must always write the range of dates but skip those that have been prescribed in another specific range X of cells, better still skip them based on the color of the cell of the X range (example 02/01/ 2021 is colored red so don't write it).

Good evening :coffee:
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For the first example I came to this after several attempts

VBA Code:
Sub date()
Dim j As Long, x As Long
    For j = 1 To Cells(2, 1) - Cells(1, 1) + 1
        Cells(j, 2) = Cells(1, 1) + x
        x = x + 1
    Next j
End Sub

someone help me for the second example?
 
Upvote 0
Hi,​
without any attachment it's just a guessing challenge …​
 
Upvote 0
Hi,​
without any attachment it's just a guessing challenge …​
Here is!


date sub.xlsm
ABCDEFGHIJ
101/01/2021second example01/01/2021
205/01/2021date to skip when execute 'cause red colour02/01/2021
303/01/2021
404/01/2021
505/01/2021
6
7
8
9
Foglio1
 
Upvote 0
Immagine.jpg
 
Upvote 0
According to your attachment a VBA beginner starter demonstration to paste to the worksheet module :​
VBA Code:
Sub Demo1()
     Dim V, Rf As Range, R&, W
         If [A2<=A1] Then Beep: Exit Sub
         Application.ScreenUpdating = False
         [A1].Copy [B1]
    With [B1].Resize([A2-A1+1])
        .Cells(1).AutoFill .Cells
         V = .Value2
    With Application.FindFormat
        .Clear
        .Interior.Color = vbRed
    With UsedRange.Columns(5)
           Set Rf = .Find("", SearchFormat:=True)
        If Not Rf Is Nothing Then
                   R = Rf.Row
            Do
                   W = Application.Match(Rf.Value2, V, 0)
                   If IsNumeric(W) Then V(W, 1) = Empty
                   Set Rf = .Find("", Rf, SearchFormat:=True)
            Loop Until Rf.Row = R
                   Set Rf = Nothing
        End If
    End With
       .Clear
    End With
       .Value2 = V
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dim V, Rf As Range, R&, W If [A2<=A1] Then Beep: Exit Sub Application.ScreenUpdating = False [A1].Copy [B1] With [B1].Resize([A2-A1+1]) .Cells(1).AutoFill .Cells V = .Value2 With Application.FindFormat .Clear .Interior.Color = vbRed With UsedRange.Columns(5) Set Rf = .Find("", SearchFormat:=True) If Not Rf Is Nothing Then R = Rf.Row Do W = Application.Match(Rf.Value2, V, 0) If IsNumeric(W) Then V(W, 1) = Empty Set Rf = .Find("", Rf, SearchFormat:=True) Loop Until Rf.Row = R Set Rf = Nothing End If End With .Clear End With .Value2 = V End With Application.ScreenUpdating = True End Sub

Works pretty well!
is there a way to eliminate the blank space so that the results are stacked one under the other anyway?
 
Upvote 0
Just add a Range.Sort statement …​
 
Upvote 0
Insert just before the last End With codeline this codeline : .Sort .Cells(1), 1, Header:=xlNo
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

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