简体   繁体   English

如何确定VBA中使用的最后一行,包括中间的空格

[英]How to determine the last Row used in VBA including blank spaces in between

How can I determine the last row in an Excel sheet, including some blank lines in the middle? 如何确定Excel工作表的最后一行,中间包括一些空白行?

With this function: 具有此功能:

Function ultimaFilaBlanco(col As String) As Long        
        Dim lastRow As Long
        With ActiveSheet
            lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, col).End(xlUp).row
        End With            
        ultimaFilaBlanco = lastRow          
End Function

And this data: 而这个数据:

Row 1 : Value
Row 2 : Value
Row 3 : Value
Row 4 : Value
Row 5 : Value
Row 6 : White
Row 7 : Value
Row 8 : Value
Row 9 : Value
Row 10  : White
Row 11  : White
Row 12  : White
Row 13  : Value
Row 14  : White

The function returns 5, and I need 13. Any idea how to do it? 该函数返回5,我需要13。任何想法怎么做?

The best way to get number of last row used is: 获取最后使用的行数的最佳方法是:

ActiveSheet.UsedRange.Rows.Count for the current sheet . 当前工作表的 ActiveSheet.UsedRange.Rows.Count

Worksheets("Sheet name").UsedRange.Rows.Count for a specific sheet . 特定 Worksheets("Sheet name").UsedRange.Rows.Count

To get number of the last column used: 获取使用的最后一列的编号:

ActiveSheet.UsedRange.Columns.Count for the current sheet . 当前工作表的 ActiveSheet.UsedRange.Columns.Count

Worksheets("Sheet name").UsedRange.Columns.Count for a specific sheet . 特定 Worksheets("Sheet name").UsedRange.Columns.Count

I hope this helps. 我希望这有帮助。

Abdo 阿卜杜

ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row -1

Short. 短。 Safe. 安全。 Fast. 快速。 Will return the last non-empty row even if there are blank lines on top of the sheet, or anywhere else. 即使工作表顶部或其他任何地方有空白行,也将返回最后一个非空行。 Works also for an empty sheet (Excel reports 1 used row on an empty sheet so the expression will be 1). 也适用于空白表(Excel报告空白表上使用了1行,因此表达式为1)。 Tested and working on Excel 2002 and Excel 2010. 经过测试并在Excel 2002和Excel 2010上工作。

I use the following: 我使用以下内容:

lastrow = ActiveSheet.Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

It'll find the last row in a specific column. 它将在特定列中找到最后一行。 If you want the last used row for any column then: 如果要为任何列使用最后使用的行,则:

lastrow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

You're really close. 你真的很亲近 Try using a large row number with End(xlUp) 尝试对End(xlUp)使用较大的行号

Function ultimaFilaBlanco(col As String) As Long

        Dim lastRow As Long
        With ActiveSheet
            lastRow = ActiveSheet.Cells(1048576, col).End(xlUp).row
        End With

        ultimaFilaBlanco = lastRow

End Function
LastRow = ActiveSheet.UsedRange.Rows.Count

The Problem is the "as string" in your function. 问题是函数中的“字符串形式”。 Replace it with "as double" or "as long" to make it work. 将其替换为“两倍”或“尽可能长”以使其起作用。 This way it even works if the last row is bigger than the "large number" proposed in the accepted answer. 这样,如果最后一行大于接受的答案中建议的“大数”,它甚至可以工作。

So this should work 所以这应该工作

Function ultimaFilaBlanco(col As double) As Long        
    Dim lastRow As Long
    With ActiveSheet
        lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, col).End(xlUp).row
    End With            
    ultimaFilaBlanco = lastRow          
End Function

Well apart from all mentioned ones, there are several other ways to find the last row or column in a worksheet or specified range. 除了所有提到的方法以外,还有其他几种方法可以找到工作表或指定范围中的最后一行或最后一列。

Function FindingLastRow(col As String) As Long

  'PURPOSE: Various ways to find the last row in a column or a range
  'ASSUMPTION: col is passed as column header name in string data type i.e. "B", "AZ" etc.

   Dim wks As Worksheet
   Dim lstRow As Long

   Set wks = ThisWorkbook.Worksheets("Sheet1") 'Please change the sheet name
   'Set wks = ActiveSheet   'or for your problem uncomment this line

   'Method #1: By Finding Last used cell in the worksheet
   lstRow = wks.Range("A1").SpecialCells(xlCellTypeLastCell).Row

   'Method #2: Using Table Range
   lstRow = wks.ListObjects("Table1").Range.Rows.Count

   'Method #3 : Manual way of selecting last Row : Ctrl + Shift + End
   lstRow = wks.Cells(wks.Rows.Count, col).End(xlUp).Row

   'Method #4: By using UsedRange
   wks.UsedRange 'Refresh UsedRange
   lstRow = wks.UsedRange.Rows(wks.UsedRange.Rows.Count).Row

   'Method #5: Using Named Range
   lstRow = wks.Range("MyNamedRange").Rows.Count

   'Method #6: Ctrl + Shift + Down (Range should be the first cell in data set)
   lstRow = wks.Range("A1").CurrentRegion.Rows.Count

   'Method #7: Using Range.Find method
   lstRow = wks.Column(col).Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

   FindingLastRow = lstRow

End Function

Note : Please use only one of the above method as it justifies your problem statement. 注意 :请仅使用上述方法之一,因为它可以说明您的问题陈述。

Please pay attention to the fact that Find method does not see cell formatting but only data, hence look for xlCellTypeLastCell if only data is important and not formatting. 请注意以下事实: Find方法看不到单元格格式,而只能看到数据,因此如果仅数据很重要而不是格式,则查找xlCellTypeLastCell。 Also, merged cells (which must be avoided) might give you unexpected results as it will give you the row number of the first cell and not the last cell in the merged cells. 此外, 合并的单元格(必须避免)可能会给您带来意想不到的结果 ,因为它将为您提供第一个单元格而不是合并后的单元格中的最后一个单元格的行号。

If sheet contains unused area on the top, UsedRange.Rows.Count is not the maximum row. 如果工作表的顶部包含未使用的区域,则UsedRange.Rows.Count不是最大的行。

This is the correct max row number. 这是正确的最大行数。

maxrow = Sheets("..name..").UsedRange.Rows(Sheets("..name..").UsedRange.Rows.Count).Row
ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.count).row

ActiveSheet可以替换为WorkSheets(1)WorkSheets("name here")

here is my code to find the next empty row for example in first row 'A'. 这是我的代码,例如在第一行“ A”中查找下一个空行。 To use it for any other row just change cells(i,2 or 3 or 4 so on) 要将其用于其他任何行,只需更改单元格(i,2或3或4,依此类推)

Sheets("Sheeet1").Select
   for i=1 to 5000
        if cells(i,1)="" then nextEmpty=i goto 20
   next i
20 'continue your code here 

enter code here

Better: 更好:

if cells(i,1)="" then 
nextEmpty=i: 
exit for

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

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