尝试复制工作表中的所有单元格时出现错误 91.但仅适用于某些代码



我正在尝试将所有数据从工作表"私有"复制到一个名为"topthree"的工作表中。

Dim r As Range
Sheets("private").Select
Set r = ActiveSheet.AutoFilter.Range
r.Select
Selection.Copy Sheets("topthree").Range("A1")

第三行给我错误:"运行时错误 91:对象变量或未设置块变量">

奇怪的是,我在宏的顶部有完全相同的代码。

Dim rng As Range
Sheets("eBS").Select
Set rng = ActiveSheet.AutoFilter.Range
r.Select
Selection.Copy Sheets("private").Range("A1")

而且该代码工作正常。这里可能有什么问题?/延斯

很可能你的AutoFilter已经关闭。

另一件事,无需使用'Select''Selection'将范围从一个工作表复制到另一个工作表,请参阅下面的修改代码:

修改后的代码

Dim r As Range
With Sheets("private")
' make sure Auto_filter is on
If .AutoFilterMode = False Then
.Range("A1").AutoFilter ' <-- in case your Auto-Filter range starts at cell "A1"
End If
Set r = .AutoFilter.Range
End With
' Copy >> Paste without selecting
r.Copy Destination:=Sheets("topthree").Range("A1")

最新更新