Testing the date in a table - can't do it row by row?

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
250
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a table that has several columns in it. I have a control sheet that has several tests on the data each time the control sheet is activated. These are typically syntax / format issues.

I originally wrote this code:

VBA Code:
Set TestCol = Tbl.HeaderRowRange.Find("Date")
TestResult = True

    For Each CellA In Tbl.DataBodyRange.Columns(TestCol.Column)
    
        If Month(CellA.Value) <> WS1.Range("RepMonth") Then
    
            TestResult = False
    
        End If

    Next CellA

    TestNow = WS1.Range("TableDates")

    If TestResult = False Then
    
        WS1.Range("TableDates") = "No"
        
    Else
        
        WS1.Range("TableDates") = "Yes"
    
    End If
    
    If WS1.Range("TableDates") <> TestNow Then WS1.Range("TableDates").Offset(0, 1) = Date

This gave me a type mismatch error and I discovered using the Immediate window that CellA had an address of A4:A230 - rather than just a specific cell.

So I thought I could define a range first, and then run a For/Each on that range. But the same result.
VBA Code:
Set TestCol = Tbl.HeaderRowRange.Find("Date")
    TestResult = True
    Set TestRange = Tbl.DataBodyRange.Columns(TestCol.Column)

    For Each CellA In TestRange
    
        If Month(CellA.Value) <> WS1.Range("RepMonth") Then
    
            TestResult = False
    
        End If

    Next CellA

    TestNow = WS1.Range("TableDates")

    If TestResult = False Then
    
        WS1.Range("TableDates") = "No"
        
    Else
        
        WS1.Range("TableDates") = "Yes"
    
    End If
    
    If WS1.Range("TableDates") <> TestNow Then WS1.Range("TableDates").Offset(0, 1) = Date

Have I misunderstood what the For Each/Next command does?

Note all references are correct and valid, and the ranges are exactly as expected.
 

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.
Try: For Each CellA In Tbl.DataBodyRange.Columns(TestCol.Column).Cells

Compare, for example:

VBA Code:
Sub DoesntWork()

    Dim r As Range
    
    For Each r In Range("A1:C3").Columns(1)
        Debug.Print r.Address
    Next r

End Sub
Sub Works()

    Dim r As Range
    
    For Each r In Range("A1:C3").Columns(1).Cells
        Debug.Print r.Address
    Next r

End Sub
 
Upvote 0
In addition to what already suggested by Stephen, above, you might use For Each CellA In Tbl.ListColumns(TestCol.Column).DataBodyRange /Next CellA
Please also note that by using Set TestCol = Tbl.HeaderRowRange.Find("Date") and later TestCol.Column you will calculate the Worksheet column, that might be different from the Table column, if the Table does not start from column A

A possible alternate method:
VBA Code:
Dim DtCol As Long
DtCol = Application.Match("Date", Tbl.HeaderRowRange, False)    'The "Date" columns


For Each CellA In Tbl.ListColumns(DtCol).DataBodyRange          '
' etc
'etc
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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