找回密码
 注册

QQ登录

只需一步,快速开始

查看: 791|回复: 0

Excel VBA -- Find last row, column or last cell.

[复制链接]
发表于 2012-8-24 10:58:56 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2012-8-25 03:17 编辑

Last used cell in one row or one column
        
        The macro's give you the row or column number of the last cell with data in one row or one column.
        Note: This code below will not work correct if the last row  with data is hidden.
  1. Sub LastRowInOneColumn()
  2. 'Find the last used row in a Column: column A in this example
  3.     Dim LastRow As Long
  4.     With ActiveSheet
  5.         LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  6.     End With
  7.     MsgBox LastRow
  8. End Sub


  9. Sub LastColumnInOneRow()
  10. 'Find the last used column in a Row: row 1 in this example
  11.     Dim LastCol As Integer
  12.     With ActiveSheet
  13.         LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
  14.     End With
  15.     MsgBox LastCol
  16. End Sub</pre>
复制代码
Last used cell in a worksheet
        
        Possible problems with xlCellTypeLastCell and UsedRange are:
        
        The last cell will only re-set when you save (or save/close/reopen the file).
        If cell formatting is changed it will not reset the last cell, clearing the data is not
        enough, you must delete the rows or columns then,
        See: http://www.contextures.com/xlfaqApp.html#Unused
        
        So when using VBA you cannot rely on this two macros if you want the last row
        or column with data on your worksheet.
        
        
        Last Row :
  1. Sub xlCellTypeLastCell_Example_Row()
  2.     Dim LastRow As Long
  3.     With ActiveSheet
  4.         LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
  5.     End With
  6.     MsgBox LastRow
  7. End Sub


  8. Sub UsedRange_Example_Row()
  9.     Dim LastRow As Long
  10.     With ActiveSheet.UsedRange
  11.         LastRow = .Rows(.Rows.Count).Row
  12.     End With
  13.     MsgBox LastRow
  14. End Sub
复制代码
Last Column :
  1. Sub xlCellTypeLastCell_Example_Column()
  2.     Dim LastColumn As Long
  3.     With ActiveSheet
  4.         LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
  5.     End With
  6.     MsgBox LastColumn
  7. End Sub


  8. Sub UsedRange_Example_Column()
  9.     Dim LastColumn As Long
  10.     With ActiveSheet.UsedRange
  11.         LastColumn = .Columns(.Columns.Count).Column
  12.     End With
  13.     MsgBox LastColumn
  14. End Sub
复制代码
Use the "Last" function to Find the last row, column or cell in range or worksheet
        
         
        In the example macros we call the function Last and this function have two arguments
         
        Argument 1 can be 1, 2 or 3
        1 = last row
        2 = last column
        3 = last cell
        Argument 2 is the range where you want to search in
        
        Copy the example macros together with the function "Last" in a normal module of your workbook.
  1. Sub LastRow_Example()
  2.     Dim LastRow As Long
  3.     Dim rng As Range

  4.     ' Use all cells on the sheet
  5.     Set rng = Sheets("Sheet1").Cells

  6.     'Use a range on the sheet
  7.     'Set rng = Sheets("Sheet1").Range("A1:D30")

  8.     ' Find the last row
  9.     LastRow = Last(1, rng)

  10.     ' After the last row with data change the value of the cell in Column A
  11.     rng.Parent.Cells(LastRow + 1, 1).Value = "Hi there"

  12. End Sub


  13. Sub LastColumn_Example()
  14.     Dim LastCol As Long
  15.     Dim rng As Range

  16.     ' Use all cells on the sheet
  17.     Set rng = Sheets("Sheet1").Cells

  18.     'Or use a range on the sheet
  19.     'Set rng = Sheets("Sheet1").Range("A1:D30")

  20.     ' Find the last column
  21.     LastCol = Last(2, rng)

  22.     ' After the last column with data change the value of the cell in row 1
  23.     rng.Parent.Cells(1, LastCol + 1).Value = "Hi there"

  24. End Sub


  25. Sub LastCell_Example()
  26.     Dim LastCell As String
  27.     Dim rng As Range

  28.     ' Use all cells on the sheet
  29.     Set rng = Sheets("Sheet1").Cells

  30.     'Or use a range on the sheet
  31.     'Set rng = Sheets("Sheet1").Range("A1:D30")

  32.     ' Find the last celll
  33.     LastCell = Last(3, rng)

  34.    ' Select from A1 till the last cell in Rng
  35.     With rng.Parent
  36.         .Select
  37.         .Range("A1", LastCell).Select
  38.     End With
  39. End Sub
  40.         
复制代码
'This is the function we use in the macro's above
  1. Function Last(choice As Long, rng As Range)
  2. 'Ron de Bruin, 5 May 2008
  3. ' 1 = last row
  4. ' 2 = last column
  5. ' 3 = last cell
  6.     Dim lrw As Long
  7.     Dim lcol As Long

  8.     Select Case choice

  9.     Case 1:
  10.         On Error Resume Next
  11.         Last = rng.Find(What:="*", _
  12.                         After:=rng.Cells(1), _
  13.                         Lookat:=xlPart, _
  14.                         LookIn:=xlFormulas, _
  15.                         SearchOrder:=xlByRows, _
  16.                         SearchDirection:=xlPrevious, _
  17.                         MatchCase:=False).Row
  18.         On Error GoTo 0

  19.     Case 2:
  20.         On Error Resume Next
  21.         Last = rng.Find(What:="*", _
  22.                         After:=rng.Cells(1), _
  23.                         Lookat:=xlPart, _
  24.                         LookIn:=xlFormulas, _
  25.                         SearchOrder:=xlByColumns, _
  26.                         SearchDirection:=xlPrevious, _
  27.                         MatchCase:=False).Column
  28.         On Error GoTo 0

  29.     Case 3:
  30.         On Error Resume Next
  31.         lrw = rng.Find(What:="*", _
  32.                        After:=rng.Cells(1), _
  33.                        Lookat:=xlPart, _
  34.                        LookIn:=xlFormulas, _
  35.                        SearchOrder:=xlByRows, _
  36.                        SearchDirection:=xlPrevious, _
  37.                        MatchCase:=False).Row
  38.         On Error GoTo 0

  39.         On Error Resume Next
  40.         lcol = rng.Find(What:="*", _
  41.                         After:=rng.Cells(1), _
  42.                         Lookat:=xlPart, _
  43.                         LookIn:=xlFormulas, _
  44.                         SearchOrder:=xlByColumns, _
  45.                         SearchDirection:=xlPrevious, _
  46.                         MatchCase:=False).Column
  47.         On Error GoTo 0

  48.         On Error Resume Next
  49.         Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
  50.         If Err.Number > 0 Then
  51.             Last = rng.Cells(1).Address(False, False)
  52.             Err.Clear
  53.         End If
  54.         On Error GoTo 0

  55.     End Select
  56. End Function
复制代码
Caveats:
        
        1: Tom Ogilvy posted this in the newsgroup
        Find does not find a cell containing the Null string "" entered when
        you do Edit=>Copy and then Edit=>aste Special, Values for a cell containing
        a formula like =IF(A1="","",A1*1.19), which may or may not be desirable (end(xlup) does).
         
        2 : Find not see cell formatting but only data, if this is important for you see the
        xlCellTypeLastCell and UsedRange section of this page to find the last row or column.
        
        3: If you use merged cells (Please do not use merged cells) maybe you get unexpected results.
        It will give you the column number of the first cell and not the last cell in the merged cells.
        
        
http://www.rondebruin.nl/last.htm
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 03:02 , Processed in 0.013945 second(s), 15 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表