VBA - Search table headers for date

cooper645

Well-known Member
Joined
Nov 16, 2013
Messages
639
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi guys,

I think I'm nearly there but can't seem to get it to do what I want.
I am trying to find the first instance of a date or date format in column headers, from left to right.

Here's the code I have managed so far, but it doesn't do as I need.

Code:
Sub InsertG1Dates()


Dim tbl As String


tbl = "_2018" 'Amend this to whatever the table name is


For i = 1 To 20
    If ActiveSheet.ListObjects(1).HeaderRowRange(i).Value = Date Then
        ActiveSheet.ListObjects(1).HeaderRowRange(i).Select
    Else
    End If
Next


End Sub

Any steer or help would be appreciated.

Thanks in advance.

Coops
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try:
Code:
Sub FindDate()
    Dim strdate As String
    strdate = CStr(Date)
    Dim foundDate As Range
    Set foundDate = Rows(1).Find(CDate(strdate), LookIn:=xlFormulas, lookat:=xlWhole)
    foundDate.Select
End Sub
 
Upvote 0
Thanks, it works if the information is not in a table, however I receive a run-time error '91'. object variable or with block variable not set
 
Upvote 0
I tried the macro on a sample table I created and it worked properly. Are the dates in row 1?
 
Upvote 0
No, Row 9, but I amended your code to suit as below.

Code:
Sub FindDate()
    Dim strdate As String
    strdate = CStr(Date)
    Dim foundDate As Range
    Set foundDate = Rows(9).Find(CDate(strdate), LookIn:=xlFormulas, lookat:=xlWhole)
    foundDate.Select 
[COLOR=#333333]End Sub[/COLOR]

My code works to find a specific text, but it needs to be dynamic and find the first string that is a date. I don't think I can format the header as a date which is my issue. maybe I need to search for a string format like "##/##/####"

I should say I am writing this on a Mac Excel 2016 but will ultimately be used on Windows Excel 2016
 
Last edited:
Upvote 0
You can format the header as a date. I tried the following on a sample table with the header row in row 1 formatted as 'Date' and it worked properly:
Code:
Sub FindDate()
    Dim lCol As Long
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Dim rng As Range
    For Each rng In Range(Cells(1, 1), Cells(1, lCol))
        If IsDate(rng) Then
            rng.Select
        End If
    Next rng
End Sub
 
Last edited:
Upvote 0
I amended it for row 9, and it works to find the furthest right date, not the furthest left. Also I can confirm I can and have formatted headers as date.

I amended my code also to work partially, it just doesn't exit upon finding the first matching string

Code:
Sub InsertG1Dates()




Dim tbl As String


tbl = "_2018" 'Amend this to whatever the table name is


For i = 1 To 20
    If ActiveSheet.ListObjects(1).HeaderRowRange(i).Value Like "##/##/####" Then
        ActiveSheet.ListObjects(1).HeaderRowRange(i).Select
    Else
    End If
Next




End Sub
 
Upvote 0
Issue solved I believe, this seems to work;

Code:
Sub InsertG1Dates()


For i = 1 To 20
    If IsDate(ActiveSheet.ListObjects(1).HeaderRowRange(i)) Then GoTo continue
    Next


continue:
ActiveSheet.ListObjects(1).HeaderRowRange(i).Select


End Sub

Thanks Mumps for your help, it gave me options to clean my code and simplify.
If you can see any reason I would have troubles I am all ears.
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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