2017-08-06 70 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

相關問題