如何查找包含有关列的数据的第一个单元格?

我在用

lastRowIndex = .Cells(.Rows.Count, "A").End(xlUp).row 

find数据的最后一行。

我如何find第一行? 我发现一些activesheet.usedrange代码,但它不适用于我,因为B列的第一个数据从第二行开始,但在A列从第四行开始。 我需要一个function,find我的数字4。

没有使用CountA()的替代版本如下:

 Function firstrow2(col As Range) As Long Dim f As Range Set f = col.Find("*", after:=col.Cells(col.Parent.Rows.Count)) '<--| look for any value in given column from its row 1 (included) downwards If Not f Is Nothing Then firstrow2 = f.Row '<--| if found then return its row index End Function 

如果传入列中没有数据,则返回0

如果你使它更健壮一点,并处理错误的通过范围(不是整列或更宽,然后一列),您可以使用以下内容:

 Function FirstRow(col As Range) As Long Dim f As Range With col.Columns(1).EntireColumn Set f = .Find("*", after:=.Cells(.Rows.Count)) '<--| look for any value from row 1 (included) End With If Not f Is Nothing Then FirstRow4 = f.Row '<--| if found then return its row index End Function 
 Dim col As Range Set col = Columns(10) If Application.CountA(col) > 0 Then Debug.Print "first data: " & col.Find("*", after:=col.Cells(Rows.Count)).Row Else Debug.Print "no data" End If