将一行复制到同一工作表中的表格底部

时间:2018-08-03 04:51:14

标签: excel-vba

我有一个VBA代码,它将在第3行中输入的数据复制到同一张表中找到的表的最后一个空行。一旦单击按钮,就会触发此复制。 使用以下代码可以正常工作:

Sub copyRow()
Dim ws As Worksheet
Dim lRow As Long

' define which worksheet to work on, i.e. replace Sheet1 with the name of 
your sheet
Set ws = ActiveWorkbook.Sheets("Record ClientsDetails")

' determine the last row with content in column D and add one
lRow = ws.Cells(Rows.Count, "D").End(xlUp).Row + 1

' copy some cells into their ranges
ws.Range("D3:F3").Copy ws.Range("D" & lRow)
ws.[D1].Select

我已经在电子表格的D列之前插入了新行,但是即使在调整代码之后,该按钮也不再复制。我收到一个错误消息:          运行时错误“ 1004”          Range类的复制方法失败

新代码为:

Sub copyRow()
Dim ws As Worksheet
Dim lRow As Long

' define which worksheet to work on, i.e. replace Sheet1 with the name of your sheet
Set ws = ActiveWorkbook.Sheets("Record ClientsDetails")

' determine the last row with content in column D and add one
lRow = ws.Cells(Rows.Count, "D").End(xlUp).Row + 1

' copy some cells into their ranges
ws.Range("D3:G3").Copy ws.Range("D" & lRow)
ws.[D1].Select

此行发生错误:     将某些单元格复制到其范围内     ws.Range(“ D3:G3”)。Copy ws.Range(“ D”&lRow)     ws。[D1]。选择

我插入的新行中具有数据验证(即,现在D3具有数据验证)。这可能是按钮不再复制第3行的原因吗?当代码在插入行之前工作时,D3没有数据验证。请告知。

0 个答案:

没有答案