简体   繁体   中英

Select the cell in a column that is not a text

I have a column with 20 rows. The header of the column is "MONTH." In the first 10 rows, the cells contain the text "Jan." In the next 10 rows, the cells contain the date, "18-12-2019". I want to write a macro that will loop through the column, starting from A2 and will stop at the cell that contains a date.

I have written the following code. But it is not working. Excel is saying, "The function is not defined." Pls. help me write the right code for this.

Sub Find_Date()
    Dim cell As Range
    Dim Rng As Range

    Set Rng = Range(("A2"), Range("A2").End(xlDown))

    For Each cell In Rng
        If IsText(cell) = True Then: cell.Offset(1, 0).Select
    Next
End Sub

To your descripted situation:

In the first 10 rows, the cells contain the text "Jan." In the next 10 rows, the cells contain the date

the following code would be a resolution:

Sub Find_Date()

    Dim row As Integer

    For row = 2 To 10
        'Do whatever you want with activesheet.cells(row,1)
    Next

End Sub

what excactly do you want to do with those cells?

Here is another solution that is more dynamic and fluid...

However, with this method you can not skip a cell row.

Sub Find_Date()
Dim row As Integer

For row = 2 To ActiveSheet.Cells(1,1).End(xlDown).Row
    'Use ActiveSheet.Cells(row, 1) to control the current cell (1 = First Column)
    If IsDate(ActiveSheet.Cells(row, 1).Value) = TrueThen
        ' Add Code Here to be executed if there is Date 
    Else
        ' Add Code Here to be executed if there is TEXT
    End If
Next

End Sub

Again, the above code will not actually do anything until modify the above.

Below is a batch of code that will simply SELECT all of the cells in column 'A' that are dates.

Sub Find_Date()
Dim row As Integer
Dim y() As Variant
Dim z As Integer
z = 1

For row = 2 To ActiveSheet.Cells(1, 1).End(xlDown).row
    If IsDate(ActiveSheet.Cells(row, 1).Value) = True Then
        ReDim Preserve y(1 To z) As Variant
        y(z) = ActiveSheet.Cells(row, 1).Address
        z = z + 1
    End If
Next
For Each x In y
    ranges = ranges + x + ", "
Next x

Range(Left(ranges, Len(ranges) - 2)).Select

End Sub

Try this:

Option Explicit

Sub test_only()         'Only for test
    Dim Rng As Range
    Dim dateCell As Range

    Set Rng = Range(("A2"), Range("A2").End(xlDown))    'Set range as desired
    Set dateCell = Find_Date(Rng)
    Debug.Print dateCell.Address                'dateCell now holds the first non-text cell (range)
End Sub

Function Find_Date(Rng As Range) As Range
    Dim cell As Range
    Set Find_Date = Nothing
    For Each cell In Rng
        If WorksheetFunction.IsText(cell) = False Then
            Set Find_Date = cell
            Exit Function
        End If
    Next
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM