第一行是所有标题,第一列是所有日期。我正在寻找vba代码/宏来查找并选择标题后每个列中的第一个非空单元格。例如,在范围B2中查找第一个非空单元格并选择它。
注意:空白和空是有区别的。
对于第一个非空(如问题标题所述)尝试:
With Columns("B")
.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
对于第一个非空(如问题正文中所示)尝试:
与上面的方法不同,这个方法还会找到公式等于空的非空单元格,例如=IF(A1=1,A1,"")
With Columns("B")
.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas).Activate
End With
.End(xlDown)
。 - Jean-François Corbetti
中,第一个非空单元格是: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
MsgBox“此列中没有数据”
应替换为MsgBox“第一个非空单元格是”& startCell.Address
。 - Jean-François Corbett