选择第二行到最后一行之上的所有值并复制并粘贴

时间:2015-12-14 19:43:35

标签: excel vba excel-vba

我正在尝试在一个过程中选择第二行到最后一行以及工作表中它上面的所有行并复制和粘贴值。

我能够在电子表格中找到最后一行并选择整行,但我很难抵消所选行。我收到了与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 :(得分: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更改为Last列。如果要复制和粘贴,将节省定义最后一列的时间。

如果您想将值复制并过去(删除公式),请执行以下操作:

'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