繁体   English   中英

Excel VBA:在Excel表中查找最后一个空单元格,而不是范围

[英]Excel VBA: find the last empty cell in an Excel table, not range

我发现以下代码不会返回该特定列中最后使用的行号。

ThisWorkbook.Sheets(1).Cells(max_row, last_data_column + 1).End(xlUp).Row

它返回的是Excel表中最后使用的行号。

我有一个范围为A1:AB142的Excel表。 在最后一行(142)中,只有列B具有数据,其余为空。 无论last_data_column是什么,上面的代码都返回142,而不是141(我尝试过22和23)。

同样,End(xlDown)不能正常工作。 即使只有W1有数据,而第一行的其余部分为空白,

ThisWorkbook.Sheets(1).Range("W1").End(xlDown).Row

当W142为空白且W1至W141不为空时,给出2。

如何在Excel表格的特定列中查找最后一个空单元格?,

使用.Find方法查找表中数据的最后一行。

然后偏移一以精简最后一个空行。

类似于(获取最后一行)的内容:

    Dim LO As ListObject
    Dim C As Range

Set LO = Sheet1.ListObjects("Table1")

With LO.Range.Columns(column_to_check) 'column_to_check is relative to the LO.Range
    Set C = .Find(what:="*", after:=.Cells(1), LookIn:=xlValues, _
        searchorder:=xlByRows, searchdirection:=xlPrevious)
    If Not C Is Nothing Then
        'do stuff`
        Debug.Print C.Row+1 'last empty row
          'If the row is the last in the table, then column is full
    End If

要找到第一个空行,类似于:

With LO.Range.Columns(1)
    Set C = .Find(what:="", after:=.Cells(1), LookIn:=xlValues, _
        searchorder:=xlByRows, searchdirection:=xlNext)
    If Not C Is Nothing Then
        'do stuff`
        Debug.Print C.Row 'First empty row
    End If
End With

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM