VBA - add in # of rows while keeping formulas and formatting

Kawule

New Member
Joined
Nov 21, 2016
Messages
23
I'm trying to see if I can modify these codes for my needs or attempt to replicate it using macro recorder.

Source codes I found:
Code:
'Original code from http://www.ozgrid.com/forum/showthread.php?t=16543
Sub AddARow()
 Dim varUserInput As Variant, RowNum
 varUserInput = InputBox("Enter Row Number where you want to add a row:", _
  "What Row?")
 If varUserInput = "" Then Exit Sub


RowNum = varUserInput
    Rows(RowNum & ":" & RowNum).Insert Shift:=xlDown
    Rows(RowNum - 1 & ":" & RowNum - 1).Copy Range("A" & RowNum)
    Range(RowNum & ":" & RowNum).ClearContents
End Sub

'http://www.ozgrid.com/forum/showthread.php?t=16543
Sub InsertRow()
    Dim Rng, n As Long, k As Long
    Application.ScreenUpdating = False
    Rng = InputBox("Enter number of rows required.")
    If Rng = "" Then Exit Sub
    Range(ActiveCell, ActiveCell.Offset(Val(Rng) - 1, 0)).EntireRow.Insert
    'need To know how many formulas To copy down.
    'Assumesfrom A over To last entry In row.
    k = ActiveCell.Offset(-1, 0).Row
    n = Cells(k, 256).End(xlToLeft).Column
    Range(Cells(k, 1), Cells(k + Val(Rng), n)).FillDown
End Sub

In my sheet I have columns going from A to BT and ideally the macro above would go into a button placed somewhere on the page.

For the 1st source code I think it actually works pretty well and all I'd change is probably the final line to paste the formulas and formatting. Is the VBA function for this XLPastespecial or something like that?

For the 2nd source code instead of using activecell I think maybe using an actual range would be better however, I'm wondering if I should make A:BT into a named range would save me troubles down the line? Also this one is closer to what I kind of want as it prompts the user for # of rows you want to add but with the current way its written you'd have to be in the row below the last one with all the contents or else it doesn't work which is my understanding on the "ActiveCell" part.
 
Last edited:

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.

Watch MrExcel Video

Forum statistics

Threads
1,133,530
Messages
5,659,358
Members
418,498
Latest member
nattynat

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
Top