我给了两种方法来找到最后一个单元格 - 使用LastCell
函数将返回这可能不是在列K.
在纸张上的最后一个单元格
我已经展示了第二种方式只是找到列K中的最后一个单元格。
然后通过给出由逗号分隔的第一个和最后一个单元格引用来设置范围。
Sub AllValues()
Dim outQuantityRange As Range
Dim currentOutQuantity As Range
Dim rLastCell As Range
With ThisWorkbook
'Find last cell on sheet containing data.
'Set rLastCell = LastCell(.Worksheets("MySheetName"))
With .Worksheets("MySheetName")
'Find last cell in column K containing data.
Set rLastCell = .Cells(.Rows.Count, 11).End(xlUp)
Set outQuantityRange = .Range("K3", rLastCell)
End With
End With
For Each currentOutQuantity In outQuantityRange
MsgBox currentOutQuantity, vbOKOnly + vbInformation
Next currentOutQuantity
End Sub
Public Function LastCell(wrkSht As Worksheet) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
列** K **常量中的条目是否为公式? –
[在VBA中查找上次使用的单元时出错]的可能重复(https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) –