VBA语法查找列中最后一个被占用的单元格

时间:2013-08-19 14:08:23

标签: excel vba syntax

如何使这项工作?我正在尝试使代码识别C列中最后使用的单元格并转到它。我在选择rng1

中的单元格的最后一部分时遇到了问题
Sub jupiter3()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("Belmont")
Set rng1 = ws.Columns("c").Find("*", ws.[c1], xlValues, , xlByRows, xlPrevious)
If Not rng1 Is Nothing Then
    MsgBox "last cell is " & rng1.Address(0, 0)
Else
    MsgBox ws.Name & " columns A:B are empty", vbCritical
End If
Range("rng1.address(0, 0)").Select

End Sub

2 个答案:

答案 0 :(得分:1)

对我来说最简单的方法就是使用这一行代码

Sheets("Belmont").Range("C" & Sheets("Belmont").Range("C" & Rows.Count).End(xlUp).Row).Select

但如果你想修改你的

Sub jupiter3()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets("Belmont")
    Set rng1 = ws.Columns("c").Find("*", ws.[c1], xlValues, , xlByRows, xlPrevious)
    If Not rng1 Is Nothing Then
        MsgBox "last cell is " & rng1.Address(0, 0)
        rng1.Select
    Else
        MsgBox ws.Name & " columns A:B are empty", vbCritical
    End If
End Sub

Range("rng1.address(0, 0)").Select更改为rng1.SelectRange(rng1.address).Select

答案 1 :(得分:0)

rng1变量被定义为一个范围,该范围由语言以不适合range()的方式解释.select你想要定义一个字符串变量并像这样做。 替换范围(“rng.address(0,0)”)。用

选择
Dim zen as String
zen=rng.address(0,0)
range(zen).select