Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在VBA中找到第一个空单元格?_Excel_Vba - Fatal编程技术网

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

Excel 如何在VBA中找到第一个空单元格?,excel,vba,Excel,Vba,我的床单看起来像: 我有一个函数来获取a列中最后一个空单元格的索引: NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1 此函数用于在第二个数组(Type2)上写入 但现在,我想要一个函数来获取a列中第一个空单元格的索引。所以我去了这个网站:我试图修改代码,但它不起作用: If Array= "Type1" Then Dim ws As Worksheet Set ws = ActiveSheet For E

我的床单看起来像:

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

NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
此函数用于在第二个数组(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"

你能帮我调整我的代码,使之具有
NextRow=IndexOf 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.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"

我这样做,它的工作:

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"

你可以用和上次一样的方法

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

你可以用和上次一样的方法

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

你应该自下而上找这个

而且
Find
优于
xlUp

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

你应该自下而上找这个

而且
Find
优于
xlUp

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

我对一些答案采取了类似的方法,但目的是反复向下看专栏,直到我能保证下面没有更多填充的单元格

我将其转化为一个小函数,并将其放入标准模块中:-

Public Function getFirstBlankRowNumberOnSheet(sht As Worksheet, Optional startingRef As String = "A1") As Long 'may get more than 32767 rows in a spreadsheet (but probably not!) Dim celTop As Range Dim celBottom As Range On Error Resume Next Set celTop = sht.Range(startingRef) Do Set celBottom = celTop.End(xlDown) Set celTop = celBottom.Offset(1) 'This will throw an error when the bottom cell is on the last available row (1048576) Loop Until IsEmpty(celBottom.value) getFirstBlankRowNumberOnSheet = celTop.Row End Function 公共函数getFirstBlankRowNumberOnSheet(sht作为工作表,可选startingRef作为String=“A1”)As Long“可能在电子表格中获得超过32767行(但可能不是!) 暗淡的celTop As范围 暗淡的CelAs范围 出错时继续下一步 Set celTop=短程(起始参考) 做 设置celBottom=celTop.End(xlDown) Set celTop=celBottom.Offset(1)'当底部单元格位于最后一个可用行(1048576)上时,将引发错误 循环直到IsEmpty(celBottom.value) getFirstBlankRowNumberOnSheet=celTop.Row 端函数
如果行#1048576中碰巧有内容,则会抛出错误!这方面的细节取决于Excel版本,我认为这是允许的最大行数。

我对一些答案采取了类似的方法,但目的是反复向下查看列,直到我可以保证下面没有更多填充的单元格

我将其转化为一个小函数,并将其放入标准模块中:-

Public Function getFirstBlankRowNumberOnSheet(sht As Worksheet, Optional startingRef As String = "A1") As Long 'may get more than 32767 rows in a spreadsheet (but probably not!) Dim celTop As Range Dim celBottom As Range On Error Resume Next Set celTop = sht.Range(startingRef) Do Set celBottom = celTop.End(xlDown) Set celTop = celBottom.Offset(1) 'This will throw an error when the bottom cell is on the last available row (1048576) Loop Until IsEmpty(celBottom.value) getFirstBlankRowNumberOnSheet = celTop.Row End Function 公共函数getFirstBlankRowNumberOnSheet(sht作为工作表,可选startingRef作为String=“A1”)As Long“可能在电子表格中获得超过32767行(但可能不是!) 暗淡的celTop As范围 暗淡的CelAs范围 出错时继续下一步 Set celTop=短程(起始参考) 做 设置celBottom=celTop.End(xlDown) Set celTop=celBottom.Offset(1)'当底部单元格位于最后一个可用行(1048576)上时,将引发错误 循环直到IsEmpty(celBottom.value) getFirstBlankRowNumberOnSheet=celTop.Row 端函数
如果行#1048576中碰巧有内容,则会抛出错误!这方面的详细信息取决于Excel版本,我想这取决于允许的最大行数。

xlDown
可能会产生误导,因为它会在一个区域中找到最后一个单元格-因此,如果A2是空白的,但使用A3,它将无法到达A3。相反,如果范围为空,它将跳转到工作表上的最后一行。
xlDown
可能会产生误导,因为它会在区域中找到最后一个单元格-因此,如果A2为空,但使用A3,它将无法到达A3。相反,如果范围为空,它将跳转到图纸上的最后一行。