VBA: copy formula after last row

Pantoffsky

New Member
Joined
Jan 26, 2016
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hi All,

Every month I need to gather a bunch of data and deliver it combined to my manufacturer. I'm trying to automate it a bit, but I'm a bit stuck. In the sheet named Work, there is a lot of data, but the amount of lines is different every month. After this data, I need to add a couple of rows from different sources. The content of these rows is created by a formula and also here, the amount of rows is different every month. So what I did now is select the first empty cell in column A and copy the formula in it:

VBA Code:
Sub addRows()
Worksheets("Work").Activate
Dim ws As Worksheet

Set ws = ActiveSheet

For Each cell In ws.Columns(1).Cells
    If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell

ActiveCell.Formula = "=""     "" &  'Report SPE'!G2 &""                "" &'Report SOC'!I2"

End Sub

My question is... how do I get this formula flexible? So, if there are 10 rows in Report SPE, create 10 rows in worksheet Work? And the cell should also be +1, so Report SPE'!G3 on the next row, Report SPE'!G4 on the next... etc.

Suggestions are welcome!
Many thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What should happen for the value from SOC report sheet? Should that be I2 then I3 etc?
 
Upvote 0
Ok, how about
VBA Code:
Sub Pantoffsky()
   Dim UsdRws As Long
   
   UsdRws = Sheets("Report SPE").Range("G" & Rows.Count).End(xlUp).Row
   Sheets("Work").Range("A" & Rows.Count).End(xlUp).Resize(UsdRws - 1).Formula = "=""     "" &  'Report SPE'!G2 &""                "" &'Report SOC'!I2"
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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