VBA - search every third column for data

macroos

New Member
Joined
May 30, 2018
Messages
45
I need a VBA in column F that grabs dates from every third column starting from I, L, O, and so on.
Columns I, L, O, etc has dates (all last day of the month) in ascending order.

For example below, column F will start with the oldest date, 2/28/17, all the way to most current date, 4/30/18.
Although there is no 7/31/17 and 8/31/17 in columns I, L, and O, I still need that in column F so that no months are skipped in column F.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD="align: center"]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]L[/TD]
[TD]M[/TD]
[TD]N[/TD]
[TD]O[/TD]
[TD]P[/TD]
[TD]Q[/TD]
[TD]R[/TD]
[TD]S[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]2/28/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]2/28/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]9/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]1/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]3/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]10/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]2/28/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]4/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]4/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]11/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]3/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]5/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]5/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]12/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]6/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]6/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD]1/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]7/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]2/28/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]8/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]9/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]4/30/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]10/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]11/30/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]12/31/17[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]2/28/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3/31/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]4/30/18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
For the layout you show in your post, this will list dates starting in F2.
Code:
Sub ListDates()
Dim last, first, mnths
last = Application.Max(Range("I:T"))
first = Application.Min(Range("I:T"))
mnths = DateDiff("m", first, last)
Application.ScreenUpdating = False
[F2] = first
With Range("F3:F" & mnths + Range("F2").Row)
    .Formula = "=EOMONTH(F2,1)"
    .Value = .Value
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
This works perfectly, but when I have formulas/numbers in the columns not containing dates, there seems to be issues.
How can this be resolved?
 
Upvote 0
Best to tell us in your initial post that columns shown as empty may in fact hold formulas/numbers ....

Here's a fix for that:
Code:
Sub ListDates()
Dim last, first, mnths, Rng As Range
Set Rng = Range("I:I,K:K,N:N,Q:Q,T:T")
last = Application.Max(Rng)
first = Application.Min(Rng)
mnths = DateDiff("m", first, last)
Application.ScreenUpdating = False
[F2] = first
With Range("F3:F" & mnths + Range("F2").Row)
    .Formula = "=EOMONTH(F2,1)"
    .Value = .Value
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
On this line: Set Rng = Range("I:I,K:K,N:N,Q:Q,T:T")
Is there a formula for it to look at every third column starting on Column I instead of listing out all the columns?
 
Upvote 0
On this line: Set Rng = Range("I:I,K:K,N:N,Q:Q,T:T")
Is there a formula for it to look at every third column starting on Column I instead of listing out all the columns?
No formula occurs to me at the moment. Here's some code that will generate the list. In this example it creates a range beginning with col I (col number 9) and adds every third column thereafter through col AA (col num 27). Change the start and end col numbers to suit as well as the step.
Code:
Function ColumnNumberToLetter(ColumnNumber As Long) As Variant
    If ColumnNumber > 16384 Then
        MsgBox "There are only 16,384 columns in Excel 2007 and later versions"
        ColumnNumberToLetter = CVErr(xlErrNA)
        Exit Function
    End If
    ColumnNumberToLetter = Split(Columns(ColumnNumber).Cells(1).Address(1, 1), "$")(1)
End Function
Sub GenerateColumnLetters()
'assume start at col I and every 3rd column until col 27
Dim ColInput As String, i As Long
For i = 9 To 27 Step 3
    x = ColumnNumberToLetter(i)
    ColInput = ColInput & "," & x & ":" & x
Next i
Set Rng = Range(Mid(ColInput, 2, Len(ColInput)))
Rng.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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