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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40301822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:25:48  来源:igfitidea点击:

How to find the first empty cell in VBA?

vbaexcel-vbaexcel

提问by Ferfa

My sheet look like :
enter image description here

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

I have a function to get index of the LASTempty cell in column A:

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

NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1

This function works to write on second array (Type2).

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

But now, i would like a function to get index of the FIRSTempty cell in column A. So i went to this website: Select first empty celland i tried to adapt code but it's doesn't work:

但是现在,我想要一个函数来获取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"

Could you help me to adapt my code to have NextRow = IndexOf FIRST empty cell in A?

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

回答by Liss

You could just use the same method you did to get the last one.

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

NextRow = Range("A1").End(xlDown).Row + 1

回答by brettdj

You should look bottom up for this.

您应该为此自下而上。

And Findis better than xlUp.

并且FindxlUp.

Sub FindBlank()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = ActiveSheet
    Set rng1 = ws.Columns(1).Find("*", ws.[a1], xlFormulas, , xlByColumns, xlPrevious)
    If Not rng1 Is Nothing Then
        MsgBox "Last used cell is " & rng1.Address(0, 0)
    Else
        MsgBox ws.Name & " row1 is completely empty", vbCritical
    End If
End Sub

回答by Ferfa

I do this and it' works:

我这样做,它的工作原理:

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.Row
                    Exit For
                    MsgBox NextRow
                End If
            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"