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

时间:2017-08-06 12:03:54

标签: excel vba excel-vba

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

代码工作正常,我完全迷失了,为什么我收到此错误

  

Object不支持此属性或方法

在线 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

2 个答案:

答案 0 :(得分:1)

或者,您可以使用Window对象的RangeSelection属性,即使选择了图形对象,也会引用工作表上的选定单元格...

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

答案 1 :(得分:0)

如果在运行宏时非范围是选择,则可能会出现此类错误。

确保选择范围,而不是某些形状,图表等