错误:对象不支持此属性或方法

我试图从一个工作簿中提取一列,并尝试将其粘贴到另一个工作簿中。

代码工作正常,我完全失去了,为什么我得到这个错误

对象不支持此属性或方法

在行LCell = Selection.SpecialCells(xlCellTypeLastCell).Address

任何人都可以帮助我,找出原因。

以下是完整的代码

Sub Extractred() Dim x As Workbook Dim y As Workbook Dim Val As Variant Dim filename As String Dim LastCell As Range Dim LastRow As Long CopyCol = Split("AK", ",") LR = Cells(Rows.Count, 1).End(xlUp).Row LC = Cells(1, Columns.Count).End(xlToLeft).Column LCell = Selection.SpecialCells(xlCellTypeLastCell).Address LCC = Selection.SpecialCells(xlCellTypeLastCell).Column lcr = Selection.SpecialCells(xlCellTypeLastCell).Row Set y = ThisWorkbook 'lcr = y.Cells(y.Rows.Count, "A").End(xlUp).Row Dim path1, Path2 path1 = ThisWorkbook.Path Path2 = path1 & "\Downloads" Set x = Workbooks.Open(filename:=Path2 & "\Red.xlsx") For Count = 0 To UBound(CopyCol) Set temp = Range(CopyCol(Count) & "1:" & CopyCol(Count) & lcr) If Count = 0 Then Set CopyRange = temp Else Set CopyRange = Union(CopyRange, temp) End If Next CopyRange.Copy y.Sheets("All").Paste y.Sheets("All").Range("B4") Application.CutCopyMode = False x.Close End Sub 

或者,您可以使用Window对象的RangeSelection属性,该对象将引用工作表上选定的单元格,即使select了一个graphics对象…

 LCell = ActiveWindow.RangeSelection.SpecialCells(xlCellTypeLastCell).Address 

如果在运行macros时select非范围,则可能会出现此类错误。

确保一个范围被选中,而不是一些形状,图表等