Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

excel VBA code for copy/paste on next row

0

hi, i have spent 2 days trying to figure this out and i give up.

all i am trying to do is have the copy/paste get entered on the next row.

i have a log on one worksheet, then i want to copy paste select cells into another sheet on the next row.

the macros runs fine, except it overwrites my first row. i need the info to be pasted on the next row.

i don't understand VBA so when i read everyone's help it makes no sense and i don't know where to input that code in my code.

anyways, help!

here is my current macros:

Sub ontario1()

'

' ontario1 Macro

'

'

    Selection.Copy

    Sheets("Ontario").Select

    ActiveCell.Offset(-1, 0).Range("A1").Select

    ActiveSheet.Paste

    Sheets("Hold Log").Select

    ActiveCell.Offset(0, 4).Range("A1").Select

    Application.CutCopyMode = False

    Selection.Copy

    Sheets("Ontario").Select

    ActiveCell.Offset(0, 1).Range("A1").Select

    ActiveSheet.Paste

    Sheets("Hold Log").Select

    ActiveCell.Offset(0, 3).Range("A1").Select

    Application.CutCopyMode = False

    Selection.Copy

    Sheets("Ontario").Select

    ActiveCell.Offset(0, 2).Range("A1").Select

    ActiveSheet.Paste

    Sheets("Hold Log").Select

    ActiveCell.Offset(0, 7).Range("A1").Select

    Application.CutCopyMode = False

    Selection.Copy

    Sheets("Ontario").Select

    ActiveCell.Offset(0, 4).Range("A1").Select

    ActiveSheet.Paste

End Sub

Thank you!

Answer
Discuss

Answers

0

Hello kcanales and welcome to the forum,

Posting your code as you have is "OK" but it is better to use code tags. To do so, click "CODE" at the top and then delete Code_Goes_Here and paste in your code. Better still is to use the "Add Files" button and attach a sample file to clearly show what you are trying to achieve.

You mention that "the macros runs fine" which I find puzzling give the errors in your code. Copying data from one sheet to another is something that is done regularly. The first thing needed in your code is to determine the next available row.

Dim LR As Long  ' last used row on worksheet 
' find the last used row; the next blank row will be LR + 1 
LR = Worksheets("Hold Log").Cells(Rows.Count, 1).End(xlUp).Row

For each cell on the "Ontario" worksheet that you want to copy to the "Hold Log" worksheet can be accomplished with one simple line of code.

Sheets("Ontario").Range("B3").Copy Sheets("Hold Log").Range("A" & LR + 1)

Repeat this line for each piece of data - update the cell references as needed.

This can also be accomplished using code which is a bit longer but easier to understand for newbies.

Worksheets("Ontario").Range("B3").Copy _
    Destination:=Worksheets("Hold Log").Range("A" & LR + 1)

In the attached file I made I have put both macros in it with buttons to click to run the macros. As you will see, it doesn't matter which sheet is active or which button you click, the result will be the same - the data will be copied from "Ontario" to "Hold Log".

If this fixes things for you please mark my answer as Selected.

Cheers   :-)

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login