2017-08-06 112 views
1

我想从一个工作簿中提取一列并尝试将其粘贴到另一个工作簿中。错误:对象不支持此属性或方法

的代码工作正常,我完全失去了,为什么我在该行收到此错误

Object does not support this property or method

LCELL = Selection.SpecialCells(xlCellTypeLastCell)。地址

谁能帮我,找出原因。

下面是完整的代码

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 

回答

0

你可以得到这种类型的错误,如果一个非范围是选择版在宏观运行的时间。

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

+0

有没有办法解决我的问题? – Jenny

+1

@珍妮 - [不要使用选择](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)。 – Jeeped

+0

@Jeeped我有疑问。我用domnik解决方案来解决问题。我现在只有几行被复制到目标工作表。我试图调试,在那段时间里,我可以看到727行被选中?你能提出什么问题 – Jenny

1

或者,您可以使用Window对象的RangeSelection属性,这将把选定的单元格在工作表上即使图形对象被选中...

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

添加此行,我得到一个“应用程序定义的错误”行Set Temp = Range(CopyCol(Count)&“1:”& CopyCol(Count)&lcr) – Jenny

+0

你能建议我如何摆脱这个? – Jenny

相关问题