how to insert rows with vba and apply formulas in excel?

excel12345u

New Member
Joined
Apr 17, 2023
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
I'm not good at VBA but I'm working on this project and I need to add VBA. I created a Minimal reproducible problem just like my real problem. I have different sections, in this case, books, books have chapters, and the number of chapters is changing. I want the VBA adds rows to the section if it has more chapters and to apply formulas from above.

I have made formulas to list chapters but either I add more rows or I need more rows. As you can see in my example below, in column b you can see how many rows I need.

O5VJF.png


Please find my Excel file with a reproducible example on the following link.

Thanks in advance, any help is more than welcome!
 

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.
  1. Press Alt + F11 to open the VBA editor.
  2. Click on "Insert" > "Module" to insert a new module.
  3. Paste the following code into the module:
Sub InsertRowsAndApplyFormulas()
Dim ws As Worksheet
Dim i As Long, lastRow As Long
Dim numRows As Long
Dim rng As Range

Set ws = ThisWorkbook.Worksheets("Sheet1") 'Replace "Sheet1" with the name of your worksheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

Application.ScreenUpdating = False

For i = lastRow To 2 Step -1
numRows = ws.Cells(i, "B").Value
If numRows > 1 Then
ws.Rows(i + 1 & ":" & i + numRows - 1).Insert Shift:=xlDown
Set rng = ws.Range(ws.Cells(i, "A"), ws.Cells(i + numRows - 1, "A"))
rng.FillDown
Set rng = ws.Range(ws.Cells(i, "C"), ws.Cells(i + numRows - 1, "C"))
rng.FillDown
End If
Next i

Application.ScreenUpdating = True
End Sub
  1. Replace "Sheet1" with the name of your worksheet that contains the data.
  2. Close the VBA editor.
  3. Press Alt + F8 to open the "Macro" dialog box, select InsertRowsAndApplyFormulas, and click "Run".
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,941
Members
449,094
Latest member
teemeren

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