2015-12-14 94 views
0

我正在嘗試在過程中選擇第二個倒數第二行以及它在工作表中的所有行並複製並粘貼值。選擇倒數第二行以上的所有值並複製並粘貼

我可以在我的電子表格中找到最後一行並選擇整行,但我很難抵消選定的行。我收到一個與offset參數有關的錯誤。

'Removes formulas above the last line in the Auto Lease Data 
Dim ALR As Long 
Dim ALR2 As Long 
With Sheets("Auto Lease Data") 
    ALR = .Range("A" & .Rows.Count).End(xlUp).Row 
    ALR2 = .Range("A" & ALR).EntireRow.Select 
    .Range(ALR2).Offset(-1).Activate 
End With 
+1

要你在哪裏粘貼值? –

+0

在同一張工作表上。謝謝。 – UnbrokenChain

+0

「複製/粘貼」之前無需「選擇」。 [見此](http://stackoverflow.com/q/10714251/445425) –

回答

1

首先你要使用在需要範圍內的數字:

'Removes formulas above the last line in the Auto Lease Data 
Dim ALR As Long 
Dim ALR2 As Range 
With Sheets("Auto Lease Data") 
    ALR = .Range("A" & .Rows.Count).End(xlUp).Row 
    Set ALR2 = .Range(ALR -1 & ":" & ALR -1) 
    ALR2.select 
End With 

現在假設你要嘗試並獲得全範圍我就改成這樣:

'Removes formulas above the last line in the Auto Lease Data 
Dim ALR As Long 
Dim ALR2 As Range 
With Sheets("Auto Lease Data") 
    ALR = .Range("A" & .Rows.Count).End(xlUp).Row 
    Set ALR2 = .Range("A1:J" & ALR -1) 
    ALR2.select 
End With 

這將選擇從A1到J列的所有內容,直到倒數第二行。將J更改爲最後一列。如果您要複製並粘貼它,將節省時間來定義最後一列。

如果要複製和過去的值回本身(刪除公式),那麼:

'Removes formulas above the last line in the Auto Lease Data 
Dim ALR As Long 
Dim ALR2 As Range 
With Sheets("Auto Lease Data") 
    ALR = .Range("A" & .Rows.Count).End(xlUp).Row 
    Set ALR2 = .Range("A1:J" & ALR -1) 
    ALR2.value = ALR2.value 
End With 
+0

嗨斯科特。謝謝你的幫助。它不幸在最後一行進行調試,不太清楚爲什麼。 – UnbrokenChain

+1

@UnbrokenChain是的我的不好,這就是我在這裏編輯而不是在VBE中得到的。請參閱編輯。 –

+0

完美,任務完成。感謝您的幫助,特別是向我展示我將數字分配給變量而不是範圍。 – UnbrokenChain