Excel 表中最后使用的单元格

Excel 表中最后使用的单元格,excel,vba,Excel,Vba,我是新来的,我希望使用Excel VBA返回工作表中最后使用的单元格 我看了看)但这并没有回答我的两个问题: .Cells.Find(…).Row方法在我的代码中花费的时间太长 我说的“上次使用的手机”可能有点奇怪……手机可能是空白的。我想取最后使用的单元格所在的列,并将其与最后使用的单元格所在的行配对 解释:假设工作表为空,但A1:C3、D2和B4中的数据除外。() 我感兴趣的最后一个单元格是D4,因为我想要工作表中包含工作表中所有数据的最后一个单元格 既然我已经解释了我要找的是什么,有

我是新来的,我希望使用Excel VBA返回工作表中最后使用的单元格

我看了看)但这并没有回答我的两个问题:

  • .Cells.Find(…).Row
    方法在我的代码中花费的时间太长

  • 我说的“上次使用的手机”可能有点奇怪……手机可能是空白的。我想取最后使用的单元格所在的列,并将其与最后使用的单元格所在的行配对

  • 解释:假设工作表为空,但
    A1:C3
    D2
    B4
    中的数据除外。()

    我感兴趣的最后一个单元格是
    D4
    ,因为我想要工作表中包含工作表中所有数据的最后一个单元格

    既然我已经解释了我要找的是什么,有人能提供关于这两个方面的建议吗

    • 如何使cells.find运行得更快或更快
    • 找到工作表中“最后一个单元格”的另一种可靠方法
    谢谢大家!

    你试过这个吗

    Dim r As Range
    Set r = Sheet1.UsedRange.SpecialCells(xlCellTypeLastCell)
    Debug.Print r.Address
    
    示例的输出:

    D$4

    已知UsedRange并不总是与实际使用的数据范围匹配。一些解决方法是使用
    CurrentRegion

    Dim r As Range
    With Sheet1.Range("A1").CurrentRegion
        Set r = Sheet1.Cells(.Rows.count, .Columns.count)
    End With
    Debug.Print r.Address
    
    此外,如果数据不是从
    A1
    开始,可能是:

    With Sheet1.Cells.Find("*").CurrentRegion
    

    使用“按行和按列查找”来标识此单元格

      Sub GetLastCellRange()
            Dim rng1 As Range
            Dim rng2 As Range
            Dim rng3 As Range
            Set rng1 = Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious)
            Set rng2 = Cells.Find("*", [a1], xlFormulas, , xlByColumns, xlPrevious)
            If Not rng1 Is Nothing Then
                Set rng3 = Range([a1], Cells(rng1.Row, rng2.Column))
                MsgBox "Range is " & rng3.Address(0, 0)
                'if you need to actual select the range (which is rare in VBA)
                Application.Goto rng3 
            Else
                MsgBox "sheet is blank", vbCritical
            End If
        End Sub
    

    虽然答案非常有效,但我会小心使用SpecialCells、CurrentRegion和所有这些

    虽然它们大部分时间都在工作,但根据我的经验,它们并不是100%可靠的

    例如,如果数据恰好有空列或空行等,CurrentRegion将不会拾取所有数据

    在我看来,最好的方法是始终在数据中使用标题。 然后可以枚举所有标题,并在该列中查找最后使用的行。然后可以确定使用的最大行,现在定义数据范围


    顺便说一句,如果您选择指定列中的最后一个单元格,则使用Range.End(xlUp)可以快速确定该列中最后使用的行,而无需循环。

    因此,请找到最后一行和最后一列,然后将它们合并到一个范围中…感谢共产国际。这就是我目前使用“.Cells.Find()”所做的,但它花费的时间太长了。想法?没有看到代码就不行。我想你需要充实这个问题。具体而言,“最后一个单元格”是否包括或排除隐藏单元格、格式化单元格(即使单元格为空)、其中包含“”的单元格(例如,
    Find(“*”
    )无法捕获)等。此外,正如@Comintern所建议的,你能给你的代码添加一些解释吗?我给代码添加了注释,解释了不同的部分。
        'function that return a range object
        Function fRNGlastCellInWorksheet(owsActive As Worksheet) As Range
        Dim dblValues As Double
        Dim dblRow As Double
        Dim dblColumn As Double
        Dim rngActive As Range
        Dim dblValuesCount As Double
        
            'total number of cells found containing a value in the whole worksheet
            dblValues = Application.WorksheetFunction.CountA(owsActive.Cells)
            dblValuesCount = 0
            
            'loop through all columns in the worksheet until the condition is met and store the column number is a variable
            For Each rngActive In owsActive.Columns
                
               'add the total number of cells found containing a value in a specific column to the total number of cells found containing a value of all previous columns
                dblValuesCount = dblValuesCount + Application.WorksheetFunction.CountA(rngActive.Cells)
    
                'if the total number of cells found containing a value in the whole worksheet is equal to the total number of cells found containing a value in all previous columns, exit the loop
                If dblValuesCount = dblValues Then
                    dblColumn = rngActive.Column
                    Exit For
                End If
            Next rngActive
            
            dblValuesCount = 0
    
            'loop through all rows in the worksheet until the condition is met and store the row number in a variable
            For Each rngActive In owsActive.Rows
                
                'add the total number of cells found containing a value in a specific row to the total number of cells found containing a value of all previous rows
                dblValuesCount = dblValuesCount + Application.WorksheetFunction.CountA(rngActive.Cells)
    
                 'if the total number of cells found containing a value in the whole worksheet is equal to the total number of cells found containing a value in all previous rows, exit the loop            
                If dblValuesCount = dblValues Then
                    dblRow = rngActive.Row
                    Exit For
                End If
            Next rngActive
            
    
            'use the variable containing the column number and the variable containing the row number to define the range cell
            Set fRNGlastCellInWorksheet = owsActive.Cells(dblRow, dblColumn)
        
        
        End Function