VBA Macro Copy to next empty row, different sheet

xenohadden

New Member
Joined
Aug 11, 2012
Messages
18
Hey guys, so I made a macro to copy data from my data entry sheet to my database sheet:


Code:
Private Sub CommandButton1_Click()
        
    Application.ScreenUpdating = False
     
    Dim NextRow As Range
    
    Set NextRow = Sheet4.Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
    
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    
    'copy to next page
    Sheets("September").Select
    Selection.PasteSpecial (xlValues), Transpose:=True


    Application.CutCopyMode = False
    Application.ScreenUpdating = True
      
    Me.Hide
    
End Sub


Private Sub CommandButton2_Click()
        
   Me.Hide
            


End Sub

The main to problems are that its not pasting in in the right collum and its not passing the next entry in the next free row.

Have attached a screenshot of the two sheets, at the sheet has some personal data, i won't be able to upload it.





Hope someone can help me wrap my head round this, been searching all over the web all day !
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,
Assuming "NextRow" is the row you want to copy to on the "September" sheet (aka Sheet4).

Code:
Dim NextRow As Range
    With Sheet4
            Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
    End With
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=True
    Application.CutCopyMode = False
    Set NextRow = Nothing
 
Upvote 0
Hi,
Assuming "NextRow" is the row you want to copy to on the "September" sheet (aka Sheet4).

Code:
Dim NextRow As Range
    With Sheet4
            Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
    End With
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=True
    Application.CutCopyMode = False
    Set NextRow = Nothing

Thank for the reply AnAnalyst, I think i've managed to confuse myself, the next row part was to try and get excel to post onto the next empty row. The normal code is:

Code:
Private Sub CommandButton1_Click()
        
    Application.ScreenUpdating = False
    
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    
    'copy to next page
    Sheets("September").Select
    Selection.PasteSpecial (xlValues), Transpose:=True
      
    Me.Hide
    
End Sub


Private Sub CommandButton2_Click()
        
   Me.Hide
            
End Sub
 
Upvote 0
Okay, so you want to paste the data to the next empty row of the sheet called "September", starting at column 2?
 
Upvote 0
Okay, so you want to paste the data to the next empty row of the sheet called "September", starting at column 2?

Yes cheer man, the first empty cell will be : B4, in the September sheet. Once the macro has copied the data across, the next push of the button should place the next lot of data on the row below.
 
Upvote 0
...in which case ...

Code:
    Dim NextRow As Range
    With Sheets("September")
        Set NextRow = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
    End With
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=True
    Application.CutCopyMode = False
    Set NextRow = Nothing
 
Upvote 0
Try this:

Code:
 Private Sub CommandButton1_Click()   

 Application.ScreenUpdating = False

    Dim NextRow As Range
    
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    
    Sheets("September").Select
    Set NextRow = Activesheet.Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
    NextRow.Select
    Selection.PasteSpecial (xlValues), Transpose:=True


    Application.CutCopyMode = False
    Application.ScreenUpdating = True
 
Last edited:
Upvote 0
...in which case ...

Code:
    Dim NextRow As Range
    With Sheets("September")
        Set NextRow = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
    End With
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=True
    Application.CutCopyMode = False
    Set NextRow = Nothing

You sir, are a pure genious, have i mentioned i love you haha! On a serious note thanks alot works perfectly now! Now only got the other bits of tidying to do, you've solved in a few minutes what i spent all day trying to fix :P
 
Upvote 0
...in which case ...

Code:
    Dim NextRow As Range
    With Sheets("September")
        Set NextRow = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
    End With
    Sheet2.Range("d3,d5,d7,d9,d11,d13,d15,d17,d19,d21,d23,d25,d27,d29,d33,d35").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=True
    Application.CutCopyMode = False
    Set NextRow = Nothing

quick question mate, how would excel handle this if it was a shared workbook? would it be best to put a force save in it before it pastes the data? Or would there be a better way round it?
 
Upvote 0
Hi again,

Personally I try to avoid using the sharing feature - I've experienced so many issues with shared workbooks!

However, if sharing the workbook is your only option you probably need to check if the workbook is in "MultiUserEditing" mode, automatically accept all changes, save the workbook, immediately run your code to update the records, and (belt and braces!) save the workbook again.

Something like the following might work for the check, update and save before you run your code, but no guarantees!

Code:
    With ThisWorkbook
        If .MultiUserEditing Then
            .AcceptAllChanges
            .Save
        End If
    End With
    'Run your code to do the paste here
    With ThisWorkbook
        If .MultiUserEditing Then
            .AcceptAllChanges
        End If
        .Save
    End With

... but like I say, I try as hard as possible to stay away from shared workbooks - I prefer to use a proper database management system for the kinds of solution that needs many users to access and update the same data.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top