简体   繁体   中英

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. 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". 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! 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.

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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