TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 250
- Office Version
- 365
- 2021
- Platform
- 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:
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.
Have I misunderstood what the For Each/Next command does?
Note all references are correct and valid, and the ranges are exactly as expected.
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.