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.

ABCDEFGHIJKLMNOPQRS
2/28/172/28/179/30/171/31/18
3/31/173/31/1710/31/172/28/18
4/30/174/30/1711/30/173/31/18
5/31/175/31/1712/31/17
6/30/176/30/171/31/18
7/31/172/28/18
8/31/173/31/18
9/30/174/30/18
10/31/17
11/30/17
12/31/17
1/31/18
2/28/18
3/31/18
4/30/18

<tbody>
</tbody>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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