在回答您的编辑
避免使用.Select/Activate
并完全限定您的对象。 INTERESTING READ
您的代码可以写成
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim rng1 As Range, rng2 As Range
'~~> Change as applicable
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set rng1 = ws.Range("A10")
Set rng2 = .Range(rng1, rng1.End(xlUp))
With rng2
Debug.Print .Address
'
'~~> Do something with the range
'
End With
End With
End Sub
如果你仍然想知道什么是你的代码错误,然后看到这一点。
您已经定义了您的范围。您不需要再次添加ActiveSheet.Range()
。您的代码可以写成
Private Sub CommandButton1_Click()
Dim i As Range
Set i = ActiveCell
ActiveSheet.Range(i, i.End(xlUp)).Select
End Sub
编辑
从评论
随访时间为ActiveSheet.Range()实际上是有问题的,或只是多余的? - user3033634 14分钟前
它是problematic
。 Range对象的默认属性是.Value
考虑这个例子,这将解释了什么问题与您的代码
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set rng = .Range("A1")
rng.Value = "Blah"
MsgBox rng '<~~ This will give you "Blah"
MsgBox rng.Value '<~~ This will give you "Blah"
MsgBox rng.Address '<~~ This will give you "$A$1"
MsgBox ws.Range(rng) '<~~ This will give you an error
'~~> Why? Becuase the above is evaluated to
'MsgBox ws.Range("Blah")
MsgBox ws.Range(rng.Address) '<~~ This will give you "Blah"
End With
End Sub
发表您的编辑,我做了一些改变,我的答案。您可能需要刷新页面才能看到它。 –