简体   繁体   English

将数据从一张纸复制到另一张纸的下一个可用行

[英]Copy data from one sheet to the next available row in another sheet

I have never used macros before, but can anyone tell what am I missing in the following macro, so the data from sheet 1 would be copied to the next available line in sheet 2? 我以前从未使用过宏,但是没有人能告诉我以下宏中缺少什么,因此工作表1中的数据将复制到工作表2中的下一个可用行? I have tried "LastRow","NextRow"commands, but I can't get it right. 我已经尝试过“ LastRow”,“ NextRow”命令,但是我做对了。 Any help would be great. 任何帮助都会很棒。

Sheets("Sheet1").Range("B2").Select
Selection.Copy
iRow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(x1up).Row + 1
Sheets("Sheet2").Range ("B" & iRow)
ActiveSheet.Paste

Try either of these. 尝试这些。 It's xlup not x1up , and you weren't pasting to your destination cell (and Selects are unnecessary). 它是xlup而不是x1up ,并且您没有粘贴到目标单元格(并且“ Selects是不必要的)。

iRow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlup).Row + 1
Sheets("Sheet1").Range("B2").Copy Sheets("Sheet2").Range ("B" & iRow)

or 要么

iRow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlup).Row + 1
Sheets("Sheet2").Range ("B" & iRow).value=Sheets("Sheet1").Range("B2").value

IN regards to this, I have the below code. 关于这一点,我有下面的代码。 However, my issue is that I have multiple versions of this macro running and pulling information to the same sheet. 但是,我的问题是我有多个版本的宏在运行并将信息拉到同一张纸上。 I need to be able to have the macro find the next row and not overwrite what is already on the page. 我需要能够让宏找到下一行,并且不覆盖页面上已经存在的内容。

Sub Product_Value_In_Specific_Row() Dim c As Range Dim J As Integer Dim Source As Worksheet Dim Target As Worksheet 子Product_Value_In_Specific_Row()Dim c作为范围Dim J作为整数Dim源作为工作表Dim目标作为工作表

' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("mongodb")
Set Target = ActiveWorkbook.Worksheets("Inputs")

J = 2     ' Start copying to row 1 in target sheet
For Each c In Source.Range("A2:A100")   ' Do 1000 rows
    If c = "Product" then
       Source.Rows(c.Row).Copy Target.Rows(J)
       J = J + 1
    End If
Next c

End Sub 结束子

I have been getting around this by changing the "J = 2" line to few rows down, but my other sheets have varying amounts of rows and would like for them to just copy and paste into the inputs sheet and not overwrite each other. 我一直在通过将“ J = 2”行向下更改几行来解决此问题,但是我的其他工作表具有不同数量的行,并且希望它们仅复制并粘贴到输入工作表中而不会彼此覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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