【问题标题】:Find bottom of Excel worksheet in VBA在 VBA 中查找 Excel 工作表的底部
【发布时间】:2021-04-27 22:48:36
【问题描述】:

我想选择工作表的底部,但不要低于使用/存储的内容。我可能有 10,000 行,但我当然没有 65,536。我不会提前知道有多少行。

在 Excel 本身中(无论如何,在最近的版本中;Excel 97 不是那么好)您可以按 Ctrl + End 转到最后一行和最后一列。我想要同样的功能。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    最简单的方法是从底部开始,然后查找包含某些内容的最后一行:
    Range("a65536").end(xlup).row

    【讨论】:

    • 除非工作表填充到第 65536 行。在 99% 的情况下,情况并非如此。我会在 range("A1).end(xlDown) 或类似的范围内执行此操作
    • 如果你往下走,XL 会在第一个空单元格处停止:通常在空单元格之后会有更多数据。如果你从底部开始往上走,你就不会有这个问题。
    【解决方案2】:

    这是基本的:

    Selection.End(xlDown).Select
    

    (在录制宏时按 ctrl + end 发现这一点。)

    【讨论】:

    • 你必须小心,如果你有一个空行,有时它只会选择它,然后忽略所有内容。我通常会抓住它到达那里的行,检查接下来的 2-3 个单元格,如果没有找到匹配项,则接受它,否则我会找到该区域的“结束”并循环,直到我有 2 或 3 个连续的空白。
    【解决方案3】:
    Public Sub Blank_Row_Remover() ' Start of Macro Code
    
    'Deletes the Entire Row within the Selection if _
    Some of the Cells Within the Selection Contain No Data.
    
    Dim Start_Cell, End_Cell, Data_Info, End_Column, This_Column As Variant
    
    Application.ScreenUpdating = False
    Application.StatusBar = "Please Stand By, ('Removing Blank Rows...' ~ Macro In Progress)..."
    
        Call Data_Info_Selection(Start_Cell, End_Cell, Data_Info, End_Column, This_Column) ' Direct Method
    
        For Each Cell In Selection
            Cell.Formula = Replace(Cell.Formula, Cell.Formula, Trim(Cell.Formula)) {Rids Extra Spaces}
            'If InStr(Cell.Value, "Labels:") Then Cell.EntireRow.Clear 'Searching for a Particular String to Remove a Row
            'If InStr(Cell.Value, "   ") Then Cell.EntireRow.Clear 'Searching for another Particular String to Remove a Row {Like 4 Spaces in a Cell that Keeps it from Reading as a Blank}
        Next
            On Error Resume Next
                Selection.SpecialCells(xlBlanks).EntireRow.Delete
            On Error GoTo 0
    
        'Call Data_Info_Selection(Start_Cell, End_Cell, Data_Info, End_Column, This_Column) ' Direct Method
    
    Application.ScreenUpdating = True
    End Sub
    Public Function Data_Info_Selection(ByRef Start_Cell, End_Cell, Data_Info, End_Column, This_Column As Variant)
    
    Application.ScreenUpdating = False
    Application.StatusBar = "Please Stand By, ('Selecting Partial Columns' ~ Macro In Progress)..."
    
    Start_Cell = ActiveCell.Address
    Start_Cell_Text = Range(Start_Cell).Text
    
    Orginal_Start_Cell = Range(Start_Cell).Address
    
    If Start_Cell_Text = "" Then
        Dim Cells As Range
            For Each Cell In Selection.Cells
                If Cell = "" Then
                    Start_Cell = Cell.Address
                Else
                    Start_Cell = Cell.Address
                    Exit For
                End If
            Next
    End If
    
        This_Column = Mid(Start_Cell, 2, 1) 'ColumnNum = ActiveCell.Column
            If Range(Start_Cell).Text = "" Then
                End_Column = This_Column & ActiveCell.Row
                End_Cell = Range(End_Column).Address
            Else
                End_Column = This_Column + "65536"
                End_Cell = Range(End_Column).End(xlUp).Address
                Start_Cell = Range(Orginal_Start_Cell).Address
            End If
    
        Data_Info = Range(Start_Cell, End_Cell).Address
        Range(Data_Info).Select
    
    End Function
    Public Sub Select_Partial_Data_Start_Cell() ' (This Seems to Work and is Cleaner and Simplier)
    
    Application.ScreenUpdating = False
    Application.StatusBar = "Please Stand By, ('Selecting Partial Data' ~ Macro In Progress)..."
    
    Dim myLastRow As Long
    Dim myLastColumn As Long
    
        Start_Cell = ActiveCell.Address
         On Error Resume Next
            myLastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
            myLastColumn = Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column
            myLast_Cell = Cells(myLastRow, myLastColumn).Address
        myRange = Start_Cell & ":" & myLast_Cell
        'If InStr(1, myLast_Cell, "104876", 1) Then myLast_Cell = "$F$1105"
    
    Range(myRange).Select
    Application.ScreenUpdating = True
    
    End Sub' End of Macro Code
    

    【讨论】:

      【解决方案4】:

      假设差距并使用来自XL: How to Determine Top/Bottom Used Cells in a Sparse Array的东西会更安全

      【讨论】:

        【解决方案5】:

        我以前用过这个

        'This finds the last row in thr worksheet
        LR = Cells(Rows.Count, 18).End(xlUp).Row
        Range(Cells(LR, 1), Cells(LR, 35)).Select
        

        【讨论】:

          【解决方案6】:

          此函数返回工作表中包含一些内容的最大行数和最大列数。也许它对sbdy有用。当然这很慢,但通常我们不必检查所有的行和列,所以我们必须调整循环。

          Public Function content_area(shName As String) As Variant
          Dim res(1 To 2) As Integer
          Dim ark As Worksheet
          
          Set ark = ThisWorkbook.Sheets(shName)
          nCol = 0
          nRow = 0
          
          For i = 1 To ark.Columns.Count
              temp = ark.Cells(ark.Cells(1, i).EntireColumn.Rows.Count, i).End(xlUp).Row
              If temp > nCol Then nCol = temp
          Next
          For i = 1 To ark.Rows.Count
              temp = ark.Cells(i, ark.Cells(i, 1).EntireRow.Columns.Count).End(xlToLeft).Column
              If temp > nRow Then nRow = temp
          Next
          
          res(1) = nCol
          res(2) = nRow
          
          content_area = res
          End Function
          

          【讨论】:

            【解决方案7】:

            '有很多方法可以做到这一点, '有些不正确,

            'the End(xlDown)SpecialCells(xlCellTypeLastCell) 等...拥有一切出错 '他们可能会跳过一些空格,因为它们被隐藏或其他原因

            '我更喜欢使用 .find 来寻找类似的东西,只是确保

            ' 最简单的方法:

            Dim LastCell   As Range
            Set LastCell = Cells(Rows.Count, Columns.Count)
            
            MsgBox LastCell.Address
            

            '(基本上是计算行数和列数,并使用这些数字来获取最后的位置)

            ' 我的主要方法:

            Dim FinalCell as range
            Set FinalCell = Cells.Find(What:="", _
                            After:=Range("a1"), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            searchorder:=xlByColumns, _
                            searchdirection:=xlPrevious, _
                            MatchCase:=False)
            msgbox FinalCell.Address
            

            '(它从单元格 a1 开始, '然后说反话,

            '因为左边或上面没有空间, '它转到工作表的末尾)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-05-07
              • 2019-09-08
              • 1970-01-01
              • 2021-12-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多