如何在VBA中找到第一个空单元格?

Fer*_*rfa 1 excel vba

我的工作表看起来像:
在此处输入图片说明

我有一个函数来获取A 列中最后一个空单元格的索引:

NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
Run Code Online (Sandbox Code Playgroud)

此函数用于写入第二个数组(Type2)。

但是现在,我想要一个函数来获取A 列中第一个空单元格的索引。所以我去了这个网站:选择第一个空单元格,我试图调整代码,但它不起作用:

If Array= "Type1" Then
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cell In ws.Columns(1).Cells
       If IsEmpty(cell) = True Then NextRow = cell: Exit For 'ERROR 1004
    Next cell
End If
If Array= "Type2" Then 'It s works
    NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
End If

ActiveSheet.Range("A" & NextRow) = "TEST"
Run Code Online (Sandbox Code Playgroud)

你能帮我调整我的代码NextRow = IndexOf FIRST empty cell in A吗?

小智 5

您可以使用与获取最后一个相同的方法。

NextRow = Range("A1").End(xlDown).Row + 1
Run Code Online (Sandbox Code Playgroud)

  • `xlDown` 可能会产生误导,因为它会找到区域中的最后一个单元格 - 因此,如果 A2 为空白但使用了 A3,则它不会到达 A3。相反,如果 range 为空,它将跳转到工作表的最后一行。 (2认同)