[英]Select the cell in a column that is not a text
I have a column with 20 rows. 我有一列有20行。 The header of the column is "MONTH."
该列的标题为“ MONTH”。 In the first 10 rows, the cells contain the text "Jan."
在前10行中,单元格包含文本“ Jan”。 In the next 10 rows, the cells contain the date, "18-12-2019".
在接下来的10行中,单元格包含日期“ 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.
我想编写一个宏,该宏将从A2开始遍历该列,并将在包含日期的单元格处停止。
I have written the following code. 我写了下面的代码。 But it is not working.
但这是行不通的。 Excel is saying, "The function is not defined."
Excel在说,“功能未定义。” Pls.
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."
在前10行中,单元格包含文本“ Jan”。 In the next 10 rows, the cells contain the date
在接下来的10行中,单元格包含日期
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. 以下是一批代码,它们将简单地选择“ A”列中所有日期的单元格。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.