duplicating rows macro

abanky

Board Regular
Joined
Apr 23, 2002
Messages
144
Office Version
  1. 2019
Platform
  1. Windows
I have a row of calculations that use cell A2 as the initial value. The calculations go from B2 to AZ2. All cells have a calculation that relies on the calculation in the cells before it.

Sometimes i have multiple calculations to run, so I drag A2:AZ2 down. So if i have 10 calculations to do, I now have the matrix A2:AZ11 set up. The only cells that are inputs are in Column A.

My question is can i set up a macro where if i put 5 in say cell A1, excel automatically drags these cells down to from my 5 row matrix?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Maybe
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) <> "A1" Then Exit Sub
   Range("A2:AZ" & Target.Value + 1).Filldown
End Sub
 
Upvote 0
Perfect thank you!!!

What if i wanted the macro to be more dynamic? Currently, if you type 1 in there, the whole row 2 disappears. Also, how can it be changed so that if i first type 10, I get nine additional rows, but if I change my mind to 8, then two rows get erased?
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) <> "A1" Then Exit Sub
   If Target.Value > 1 Then
      Range("3:" & Rows.Count).ClearContents
      Range("A2:AZ" & Target.Value + 1).Filldown
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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