我是新来的,我希望使用Excel VBA返回工作表中最后使用过的单元格。
我查看了Error in finding last used cell in VBA),但这并没有回答我遇到的两个问题:
.Cells.Find(...).Row
方法在我的代码中花费的时间太长了。
我的意思是"最后使用的细胞"可能很奇怪......细胞可能是空白的。我想获取具有最后一个使用过的单元格的列,并将其与具有最后一个使用过的单元格的行配对。
解释:假设工作表为空,但A1:C3
,D2
和B4
中的数据除外。 (Exhibit 1)
我感兴趣的最后一个单元格是D4
,因为我想要工作表中包含工作表中所有数据的最后一个单元格。
既然我已经解释了我要找的东西,那么任何人都可以提供关于
的建议吗?谢谢!
答案 0 :(得分:4)
你试过这个吗?
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
答案 1 :(得分:1)
按行和列使用Find
来标识此单元格。
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
答案 2 :(得分:0)
'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
答案 3 :(得分:-1)
虽然答案非常有效,但我会小心使用SpecialCells,CurrentRegion以及所有这些。
虽然他们大部分时间都在工作,但根据我的经验,他们并非100%可靠。
例如,如果数据恰好有空列或行等,CurrentRegion将不会获取所有数据。
在我看来,最好的方法是使用标题和数据。 然后,您可以枚举所有标头并找到该列中最后使用的行。 然后,您可以确定使用的最大行,现在定义数据范围。
顺便说一下,如果你选择指定列中的最后一个单元格然后使用Range.End(xlUp),你可以快速确定没有循环的那一列的最后一行。
希望有所帮助。