简体   繁体   English

将粘贴数据复制到第一个空行 VBA

[英]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.您的帮助将不胜感激。

Receiving an error收到错误在此处输入图像描述

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

Copy by Assignment按作业复制

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:您的代码有几个问题:

  • You don't seem to have a variable Sheet2 declared, however you are using it - it will not compile (unless it's a global/module variable)您似乎没有声明变量 Sheet2,但是您正在使用它 - 它不会编译(除非它是全局/模块变量)
  • You cannot put such a string ("G7" & "H7" &...) in Range method as a parameter.您不能将这样的字符串 ("G7" & "H7" &...) 作为参数放在 Range 方法中。 Try replacing it by Range("G7:N7"), which refers to all cells G7 through N7.尝试用 Range("G7:N7") 替换它,它指的是 G7 到 N7 的所有单元格。 IF you need descrete ranges to be copied either copy them one by one or use Union.如果您需要复制离散范围,请一一复制或使用联合。
  • In line 6 you just state Sheet2.Range(...) - what't the reason behind this.在第 6 行中,您只需 state Sheet2.Range(...) - 这背后的原因是什么。 It does nothing.它什么也不做。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM