2015-12-14 93 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