简体   繁体   English

当一张工作表合并单元格时用于从一张Excel工作表复制到另一张工作表的宏

[英]Macro for copying from one excel sheet to another when one sheet has merged cells

I have been working on this template in Excel 2013 for some time now. 我已经在Excel 2013中处理此模板已有一段时间了。 I have used several different suggestions from here and other sites and still can't get this to work. 我从这里和其他站点使用了几种不同的建议,但仍然无法使它起作用。 In the ideal world this would be a template that I print the first sheet from after entering my data, hit the macro button, and then it saves the certain cell data to the next sheet, named "log". 在理想情况下,这将是一个模板,我在输入数据后从第一张工作表中打印出来,单击宏按钮,然后将某些单元格数据保存到名为“ log”的下一张工作表中。 The data in the log will just compile over time, whereas the main sheet will be changed and then printed as a data sheet. 日志中的数据将随时间推移进行编译,而主表将被更改,然后作为数据表打印。

For some reason using this code my data always ends up in the last row of the columns. 出于某种原因,使用此代码,我的数据总是在列的最后一行结束。 I am also having trouble getting it to keep adding data after running the macro the first time. 我也很难让它在第一次运行宏后继续添加数据。 I used names instead of cell ranges because I thought that may help excel deal with the merged cells that I am copying from. 我使用名称而不是单元格范围,因为我认为这可能有助于擅长处理从中复制的合并单元格。 It also throws an error once the columns are populated on the log sheet. 一旦在日志表中填充了列,它也会引发错误。 Any advice would be great as I am not very well versed in vba! 任何建议都是很好的,因为我对vba不太了解! If it would be easier to send these to a log from a Word template that would work for me as well but I thought it would be easier to work only within excel. 如果将它们从同样适用于我的Word模板发送到日志更容易,但我认为仅在excel中工作会更容易。

Thanks!!! 谢谢!!!

Sub Paste()
Dim LastRow As Long

LastRow = Sheets("log").Range("A65536").End(xlUp).row + 1

Sheets("Sheet").Range("ManufactureDate").Copy Destination:=Sheets("log").Range("A2" & LastRow)
Sheets("Sheet").Range("PartNumber").Copy Destination:=Sheets("log").Range("B2" & LastRow)
Sheets("Sheet").Range("SampleNumber").Copy Destination:=Sheets("log").Range("C2" & LastRow)
Sheets("Sheet").Range("ShiftNumber").Copy Destination:=Sheets("log").Range("D2" & LastRow)
Sheets("Sheet").Range("BatchNumber").Copy Destination:=Sheets("log").Range("E2" & LastRow)
Sheets("Sheet").Range("LineNumber").Copy Destination:=Sheets("log").Range("F2" & LastRow)
Sheets("Sheet").Range("AverageHardness").Copy Destination:=Sheets("log").Range("G2" & LastRow)
Sheets("Sheet").Range("TotalCompression").Copy Destination:=Sheets("log").Range("H2" & LastRow)
Sheets("Sheet").Range("Length").Copy Destination:=Sheets("log").Range("I2" & LastRow)
Sheets("Sheet").Range("PostWidth").Copy Destination:=Sheets("log").Range("J2" & LastRow)
Sheets("Sheet").Range("RailWidth").Copy Destination:=Sheets("log").Range("K2" & LastRow)
Sheets("Sheet").Range("TotalDepth").Copy Destination:=Sheets("log").Range("L2" & LastRow)
Sheets("Sheet").Range("Offset").Copy Destination:=Sheets("log").Range("M2" & LastRow)

End Sub

I do not believe your variable LastRow was defined properly. 我认为您的变量LastRow定义不正确。 Try using the code below. 尝试使用下面的代码。 I tried it and it works! 我试过了,它有效! The code below copy's the cell contents of cell A1 on Sheet1 and paste's it on sheet Log into the first empty cell in column A. You can easily extend the code to your situation. 下面的代码复制Sheet1上单元格A1的单元格内容,并将其粘贴到工作表上。登录到列A中的第一个空单元格中。您可以轻松地将代码扩展到您的情况。 Hope this helps. 希望这可以帮助。

Sub Paste2log()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ThisWorkbook.Worksheets("log")

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

Sheets("Sheet1").Range("A1").Copy Destination:=sht.Range("A" & LastRow + 1)

End Sub

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

相关问题 Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? 库存宏-将信息从一页纸复制到另一页 - Inventory Macro - copying information from one row of sheet to another sheet Excel宏,用于从一个工作表中的特定字段复制数据,仅保留该值并将其添加到另一工作表中 - Excel macro for copying data from a specific field in one sheet keeping only the value and adding it to another sheet VBA从一个Excel工作表复制到另一个Excel工作表? - VBA copying from one excel sheet to another excel sheet? VBA代码 - 根据匹配条件将单元格中的单元格复制到Excel中的另一个工作表 - VBA Code - Copying cells from one sheet to another sheet in excel depending on matching condition 将excel行从一个工作表复制到另一个工作表 - Copying excel row from one sheet to another 将Excel中的Excel数据复制到另一个工作表中 - Copying excel data from one sheet into another sheet Excel宏-将数据从一张纸复制到下一张纸 - Excel Macro - Copying data from one sheet to the next 1004将范围从一个工作表复制到另一个工作表时出错 - 1004 Error when copying range from one sheet to another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM