如何find列中的第一个非空单元格?

第一行是所有标题,第一列是所有date。 我正在寻找VBA代码/macros来查找并select头后的每个列中的第一个非空的单元格。 例如find范围B2中的第一个非空单元格并将其选中。

如果你的第一行是一个标题行,那么你可以使用:

Columns("B").SpecialCells(xlCellTypeBlanks)(1).Select 

编辑

很抱歉误读了这个问题,上面的代码是完全错误的。 一个办法:

注意:它们是空白和空白的区别。

对于第一个非空白(如问题标题)尝试:

 With Columns("B") .Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate End With 

对于第一个非空(如问题身体)尝试:

与上面不同的是,这也会find一个公式等于空白的非空单元格,即= IF(A1 = 1,A1,“”)

 With Columns("B") .Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas).Activate End With 

在每一列中,第一个非空单元格是

 set first_ne = cells(2,i) if isempty(first_ne.value) then set first_ne = first_ne.end(xldown) end if 

使用IsEmpty函数可以检查一个单元格是否为空白:

 Sub GetFirstNonEmptyCell() Dim startCell as range, firstNonEmptyCell as range Set startCell = Range("B2") 'change this depending on which column you are looking at If VBA.IsEmpty(startCell.Value) Then MsgBox "No data in this column" Else Set firstNonEmptyCell = startCell.End(xlDown) MsgBox "First non empty cell is " & firstNonEmptyCell.Address End If End Sub