ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5004|回复: 1

[转帖] Excel & VBa: find last row, column, cell in an Excel (work)sheet

[复制链接]

TA的精华主题

TA的得分主题

发表于 2010-6-30 20:39 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Source:  
      http://www.ozgrid.com/VBA/ExcelRanges.htm : [Goto this site ]
        
      The following page contains some usefull (general) vba code that can be
      used to find the last row, column and/or cell in an Excel (work)sheet.
      Find the last used cell, before a blank in a Column:
      Sub LastCellBeforeBlankInColumn()
        Range("A1").End(xldown).Select
      End Sub

      Find the very last used cell in a Column:
      Sub LastCellInColumn()
        Range("A65536").End(xlup).Select
      End Sub
     
      Find the last cell, before a blank in a Row:
      Sub LastCellBeforeBlankInRow()
        Range("A1").End(xlToRight).Select
      End Sub

      Find the very last used cell in a Row:
      Sub LastCellInRow()
        Range("IV1").End(xlToLeft).Select
      End Sub

      Find the very last used cell on a Worksheet:
      Sub Demo()
        Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Select
      End Sub

        
      Find the last used Row on a Worksheet:
      Sub FindLastRow()
        Dim LastRow As Long

        If WorksheetFunction.CountA(Cells) > 0 Then
          'Search for any entry, by searching backwards by Rows.
          LastRow = Cells.Find(What:="*", After:=[A1], _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlPrevious).Row
          MsgBox LastRow
        End If
      End Sub

      Find the last used Column on a Worksheet:
      Sub FindLastColumn()
        Dim LastColumn As Integer
        If WorksheetFunction.CountA(Cells) > 0 Then
          'Search for any entry, by searching backwards by Columns.
          LastColumn = Cells.Find(What:="*", After:=[A1], _
                                  SearchOrder:=xlByColumns, _
                                  SearchDirection:=xlPrevious).Column
          MsgBox LastColumn
        End If
      End Sub
        
      Find the last used Cell on a Worksheet:
      Private Sub FindLastCell()
        Dim LastColumn As Integer
        Dim LastRow As Long
        Dim LastCell As Range
        If WorksheetFunction.CountA(Cells) > 0 Then
          'Search for any entry, by searching backwards by Rows.
          LastRow = Cells.Find(What:="*", After:=[A1], _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlPrevious).Row
          'Search for any entry, by searching backwards by Columns.
          LastColumn = Cells.Find(What:="*", After:=[A1], _
                                  SearchOrder:=xlByColumns, _
                                  SearchDirection:=xlPrevious).Column
          MsgBox Cells(LastRow, LastColumn).Address
        End If
      End Sub  
        
      Add a row at each change in a column.
      Assume you have a long list of data and you want to insert a row at each
      change. While you could use a simple Loop this method is much faster. The
      Data must be sorted!
        
      Option Explicit

      Private Sub InsertRowAtEachChange()
        Dim objRange As Excel.Range

        ''' On error goto the error handler defined in the lower part of this
      function.
        On Error GoTo ErrHandler

        ''' Ensure an entire Column is selected
        If Selection.Cells.Count <> 65536 Then
          Call MsgBox("You must select an entire column", vbCritical)
          End
        End If
        
        '''Set a range variable to all data in selected column
        Set objRange = Range(Selection.Cells(2, 1), Selection.Cells(65536,
      1).End(xlUp))

        ''' Add a column for formulas
        With objRange
          .EntireColumn.Insert
          .Offset(0, -1).FormulaR1C1 = "=IF(AND(NOT(ISNA(R[-1]C))," & _
                                       "R[-1]C[1]<>RC[1]),0,"""")"
          ''' Convert to values
          .Offset(0, -1) = .Offset(0, -1).Value
          ''' Set variable to 0
          Set objRange = .Offset(0, -1).SpecialCells(xlCellTypeConstants,
      xlNumbers)
        End With
         
        ''' Add a row at each 0
        If WorksheetFunction.CountIf(objRange, 0) > 0 Then
          Call objRange.EntireRow.Insert
        End If
        
        ''' Reset variable for next formulas
        Set objRange = Range(Selection.Cells(2, 1), Selection.Cells(65536,
      1).End(xlUp))
        
        ''' Add the formula to add 0
        objRange.FormulaR1C1 = "=IF(OR(RC[1]="""",R[-1]C[1]=""""),""""," & _
                               "IF(RC[1]<>R[-1]C[1],0))"
        ''' Convert to values
        objRange = objRange.Value
        
        ''' Set variable to 0 cells if any
        If WorksheetFunction.CountIf(objRange, 0) > 0 Then
          Set objRange = objRange.SpecialCells(xlCellTypeConstants, xlNumbers)
          ''' Add a row at each 0
          objRange.EntireRow.Insert
        End If
        
        ''' Delete added Column
        Call objRange.Columns(1).EntireColumn.Delete

        ''' Remove an instance
        Set objRange = Nothing

        ''' Exit the Sub
        Exit Sub
         
      ''' Define the subfunction for errorhandling.
      ErrHandler:
        ''' Error.
        '''Call gobjLogFile.Error("ThisWorkbook, function :
      InsertRowAtEachChange", _
                               "Description goes here", Err.Number,
      Err.Description)
        
        ''' Resume anyway.
        Resume Next

      End Sub         
      Last Updated ( Friday, 22 February 2008 )

[ 本帖最后由 marko1981 于 2010-7-1 06:19 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-6-30 21:20 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-1 09:41 , Processed in 0.031804 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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