[英]Copy Paste Data into First empty Row VBA
I have wrote a below code which is not working.我写了一个下面的代码,它不起作用。
The thing i want from this code is to copy the Sheet4
cells
and paste them into the Sheet2
given cells
in the first empty row.我想从此代码中复制Sheet4
cells
并将它们粘贴到第一个空行中的Sheet2
给定cells
中。
I have tried to find an way but nothing comes which could help.我试图找到一种方法,但没有任何帮助。 Your help will be appreciated.您的帮助将不胜感激。
Sub Save()
Dim NextRow As Range
Set NextRow = Range("A" & Sheets("Sheet2").UsedRange.Rows.Count + 1)
Sheet4.Range("G7" & "H7" & "I7" & "J7" & "K7" & "L7" & "M7" & "N7").Copy
Sheet2.Activate
Sheet2.Range ("A2" & "C2" & "E2" & "F2" & "H2" & "J2" & "L2" & "M2")
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
But it does not go for the next Row over running the code.但它不会 go 用于运行代码的下一行。
Sub Get_Data()
With Sheet4
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
Sheet4.Range("G7").Copy
Sheet2.Range("A" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("H7").Copy
Sheet2.Range("C" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("I7").Copy
Sheet2.Range("E" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("J7").Copy
Sheet2.Range("F" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("K7").Copy
Sheet2.Range("H" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("L7").Copy
Sheet2.Range("J" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("M7").Copy
Sheet2.Range("L" & lastrow).PasteSpecial xlPasteValues
Sheet4.Range("N7").Copy
Sheet2.Range("M" & lastrow).PasteSpecial xlPasteValues
End Sub
Option Explicit
Sub copyByAssignment()
' Constants (Destination)
Const dColsList As String = "A,C,E,F,H,J,L,M"
' Source
Dim srg As Range: Set srg = Sheet4.Range("G7:N7")
' Destination
Dim dRow As Long
dRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1
Dim dCols() As String: dCols = Split(dColsList, ",") ' zero-based '(i - 1)'
' Copy by Assignment
Dim i As Long
For i = 1 To srg.Cells.Count
Sheet2.Cells(dRow, dCols(i - 1)).Value = srg.Cells(i)
Next i
End Sub
EDIT:编辑:
Sub copyByAssignmentLists()
' Constants
Const sRow As Long = 7
Const sColsList As String = "A,C,E,F,H,J,L,M"
Const dColsList As String = "A,C,E,F,H,J,L,M"
' Source
Dim sCols() As String: sCols = Split(sColsList, ",")
' Destination
Dim dCols() As String: dCols = Split(dColsList, ",")
Dim dRow As Long
dRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1
' Copy by Assignment
Dim n As Long
For n = 0 To UBound(sCols)
Sheet2.Cells(dRow, dCols(n)).Value = Sheet4.Cells(sRow, sCols(n)).Value
Next n
End Sub
There are a couple of issues with your code:您的代码有几个问题:
I would guess that the use of Range is the error VBA complains about.我猜想 Range 的使用是 VBA 抱怨的错误。 However, I would recommend to see a ready working solution and try to understand it or read about the use of methods you use.但是,我建议您查看现成的工作解决方案并尝试理解它或阅读有关您使用的方法的使用。 Not to repeat other SO threads, here's a good one: How do I copy a range of formula values and paste them to a specific range in another sheet?不要重复其他 SO 线程,这是一个很好的线程: 如何复制一系列公式值并将它们粘贴到另一张表中的特定范围?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.