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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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