Paste on next empty row

MPFraser7

New Member
Joined
Dec 14, 2016
Messages
34
Hello,

I'm currently using the following code to copy and paste a range of cells from one workbook to another. Everything is working as intended but next quarter, I need to run this macro again but have the quarter 2 results pasted on the next empty row. If I was to run it again the way it's currently set up, it would obviously overwrite quarter 1 results. How can I change my macro so that it doesn't overwrite what's already in there but rather paste starting on next empty row? Thanks!

Sub CommandButton1_Click()
Dim strfilename As String

For Each wb In Application.Workbooks
If wb.Name <> ActiveWorkbook.Name Then
strfilename = wb.Name
Debug.Print strfilename
End If
Next
Application.ScreenUpdating = False
Windows(strfilename).Activate
Sheets("Non Salary Spending").Select
Range("A13:N400").Select
Selection.Copy
Windows("PivotTool.xlsm").Activate
Sheets("NonSalaryByPeriod").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this:
Code:
Sub CommandButton1_Click()

    Dim strfilename As String
    Dim nextRow As Long

    For Each wb In Application.Workbooks
        If wb.Name <> ActiveWorkbook.Name Then
            strfilename = wb.Name
            Debug.Print strfilename
        End If
    Next
    
    Application.ScreenUpdating = False
    Windows(strfilename).Activate
    Sheets("Non Salary Spending").Select
    Range("A13:N400").Copy
    Windows("PivotTool.xlsm").Activate
    Sheets("NonSalaryByPeriod").Select
    [COLOR=#ff0000]nextRow = Cells(Rows.Count, "B").End(xlUp).Row + 1[/COLOR]
    Range("B" & [COLOR=#ff0000]nextRow[/COLOR]).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 
Upvote 0
Range("B65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1).Select
All those extra Selects are not necessary.
You can combine all that into this:
Range("B65536").End(xlUp).Offset(1).Select

Note that the row 65536 is a holdover from the OLD Excel (xls) format that only allows 65536 rows. Newer versions of Excel (not so new really, 14 years old now!) allow for over a million.
So using Rows.Count will return however many rows that version of Excel has (so it is dynamic).
To see how to use it, see my code above in the first reply.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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