vba ActiveSheet.Range(Cells(x,y), Cells(w, z)).Select 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30358208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:35:39  来源:igfitidea点击:

ActiveSheet.Range(Cells(x,y), Cells(w, z)).Select not working

excelvbaexcel-vba

提问by oitathi

Could anyone tell me why when I refer to a particular range it works fine:

谁能告诉我为什么当我提到特定范围时它工作正常:

ActiveSheet.Range("A1:D3").Select

but

ActiveSheet.Range(Cells(1, 1), Cells(3, 4)).Select

not working?

不工作?

回答by Rory

I suspect your code is in the worksheet code module of a different sheet, so the unqualified Cellscalls refer to thatsheet, not the active one. You should alwaysqualify allRangeor Cellscalls with a Worksheetobject:

我怀疑您的代码位于不同工作表的工作表代码模块中,因此不合格的Cells调用是指该工作表,而不是活动的工作表。您应该始终使用对象限定allRangeCells调用Worksheet

ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, 4)).Select

This also works avoiding the need to repeat several times the target worksheet: (see https://msdn.microsoft.com/EN-US/library/office/gg264723.aspx)

这也可以避免重复多次目标工作表的需要:(参见https://msdn.microsoft.com/EN-US/library/office/gg264723.aspx

With ActiveSheet
    .Range(.Cells(1, 1), .Cells(3, 4)).Select
End With