48

A列有这样的数据(即频繁的空白单元格):

HEADING  <-- this is A1
kfdsl
fdjgnm
fdkj

gdfkj
4353

fdjk  <-- this is A9

我希望能够获得最后一个有数据的单元格的单元格引用。所以在上面的例子中,我想返回:A9

我试过了,但它停在第一个空白单元格(即返回A4

numofrows = destsheet.Range("A2").End(xlDown).Row - 1
4

10 回答 10

49

我喜欢这种方式:

ActiveSheet.UsedRange.Rows.Count

列数也可以这样做。对我来说,总是工作。但是,如果您在另一列中有数据,上面的代码也会考虑它们,因为代码正在查找工作表中的所有单元格范围。

于 2011-06-15T10:33:09.263 回答
41

最安全的选择是

Lastrow =  Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
Lastcol =  Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column

不要使用UsedRangeorSpecialCells(xlLastCell)End(xlUp)。如果您之前删除了一些行,所有这些方法都可能会给出错误的结果。Excel 仍然计算这些不可见的单元格。

如果您删除单元格、保存工作簿、关闭并重新打开它,这些方法将再次起作用。

于 2013-01-18T00:28:45.340 回答
26

这将独立于 Excel 版本(2003、2007、2010)。第一个在一张表中有 65536 行,而后两个有大约一百万行。Sheet1.Rows.Count根据版本返回此数字。

numofrows = Sheet1.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row

或同等但更短的

numofrows = Sheet1.Cells(Sheet1.Rows.Count,1).End(xlUp)

这将从 A 列的底部向上搜索第一个非空单元格,并获取其行号。

如果您的数据在其他列中进一步下降,这也适用。因此,例如,如果您获取示例数据并在单元格 FY4763 中写入一些内容,则上述内容仍将正确返回 9(不是 4763,任何涉及该UsedRange属性的方法都会错误地返回)。

请注意,实际上,如果您想要单元格引用,您应该只使用以下内容。您不必先获取行号,然后再构建单元格引用。

Set rngLastCell = Sheet1.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp)

请注意,此方法在某些极端情况下会失败:

  • 最后一行包含数据
  • 最后一行被隐藏或过滤掉

因此,请注意您是否打算将第 1,048,576 行用于这些事情!

于 2011-06-10T09:24:16.667 回答
19

我将所有可能性与一张长测试表进行了比较:

0,140625 秒

lastrow = calcws.Cells.Find("*", [A1], , , xlByColumns, xlPrevious).row

0 秒

iLastRow = calcws.Cells(rows.count, "a").End(xlUp).row

numofrows = calcws.Cells.SpecialCells(xlLastCell).row

0,0078125 秒

lastrow = calcws.UsedRange.rows.count
Do While 1
    If calcws.Cells(lastrow, 1).Value = "" Then
        lastrow = lastrow - 1
    Else
        Exit Do
    End If
Loop

我认为最爱是显而易见的......

于 2013-02-24T13:43:18.317 回答
6

Dim RowNumber As Integer
RowNumber = ActiveSheet.Range("A65536").End(xlUp).Row

在您的情况下,它应该返回 #9

于 2012-11-01T17:51:19.610 回答
5

在另一个站点上找到了这种方法。它适用于新的更大尺寸的 Excel,并且不需要您硬编码最大行数和列数。

iLastRow = Cells(Rows.Count, "a").End(xlUp).Row
iLastCol = Cells(i, Columns.Count).End(xlToLeft).Column

感谢澳大利亚梅尔本的 mudraker

于 2013-01-04T19:49:45.383 回答
3

这些都可以工作,让 Excel 定义它最后一次看到数据

numofrows = destsheet.UsedRange.SpecialCells(xlLastCell).row

numofrows = destsheet.Cells.SpecialCells(xlLastCell).row
于 2011-06-10T05:22:41.367 回答
2
  n = ThisWorkbook.Worksheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
于 2016-08-15T17:46:34.567 回答
1

为了更清楚起见,我想添加一个清晰的示例并运行

            openFileDialog1.FileName = "Select File"; 
            openFileDialog1.DefaultExt = ".xls"; 
            openFileDialog1.Filter = "Excel documents (.xls)|*.xls"; 


            DialogResult result = openFileDialog1.ShowDialog();


            if (result==DialogResult.OK)
            {

                string filename = openFileDialog1.FileName;


                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Excel.Application();
                xlApp.Visible = false;
                xlApp.DisplayAlerts = false;



                xlWorkBook = xlApp.Workbooks.Open(filename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                var numRows = xlWorkSheet.Range["A1"].Offset[xlWorkSheet.Rows.Count - 1, 0].End[Excel.XlDirection.xlUp].Row;

                MessageBox.Show("Number of max row is : "+ numRows.ToString());

                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

            }
于 2014-10-20T08:58:03.093 回答
1

我更喜欢使用 CurrentRegion 属性,它相当于 Ctrl-*,它将当前范围扩展到其最大的连续数据范围。您从您知道将包含数据的单元格或范围开始,然后将其展开。UsedRange 属性有时会返回很大的区域,只是因为有人在工作表底部进行了一些格式化。

Dim Liste As Worksheet    
Set Liste = wb.Worksheets("B Leistungen (Liste)")     
Dim longlastrow As Long
longlastrow = Liste.Range(Liste.Cells(4, 1), Liste.Cells(6, 3)).CurrentRegion.Rows.Count
于 2013-10-21T09:12:20.147 回答