VBA Code for Inserting Rows Based on Cell Value

VeryBasicAtExcel

New Member
Joined
Aug 13, 2019
Messages
3
Hello all,

I am trying to create a macro that will perform some very basic formatting. Essentially, I have a dynamic list of records that I want to display under relevant headers. I'm using a counta formula to determine how many records exist under each category. I then need the macro to insert rows underneath the header, based on that counta value. If anyone can offer some assistance or get me going in the right direction, that would be most appreciated!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Not sure how to edit my original post.
A | B
[TABLE="width: 195"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD="align: right"]2 |[/TD]
[TD]IQ1[/TD]
[/TR]
[TR]
[TD="align: right"]1 |[/TD]
[TD]IQ2[/TD]
[/TR]
[TR]
[TD="align: right"]2 |
[/TD]
[TD]IQ3[/TD]
[/TR]
[TR]
[TD="align: right"]2 |[/TD]
[TD]IQ4[/TD]
[/TR]
</tbody>[/TABLE]


That is essentially what the data looks like. I want the macro to insert 2 rows below IQ4, 2 rows below IQ3, 1 row below IQ2, and 2 rows below IQ1. It needs to be able to handle longer lists with more variation.
The values in both columns will change, however column B data will always begin with "IQ".
 
Upvote 0
Try working with this code and customize it to your needs. If you have questions you may reach me at (123 davidrhodes at gmail dot com)

If ActiveCell.Value = "IQ4" Then
ActiveCell.Offset(1, 0).Select 'Go down one row same column
ActiveCell.EntireRow.Insert 'Insert row
ActiveCell.EntireRow.Insert 'Insert row
ElseIf ActiveCell.Value = "IQ3" Then
ActiveCell.EntireRow.Insert 'Insert row
End If
 
Upvote 0
Try working with this code and customize it to your needs. If you have questions you may reach me at (123 davidrhodes at gmail dot com)

If ActiveCell.Value = "IQ4" Then
ActiveCell.Offset(1, 0).Select 'Go down one row same column
ActiveCell.EntireRow.Insert 'Insert row
ActiveCell.EntireRow.Insert 'Insert row
ElseIf ActiveCell.Value = "IQ3" Then
ActiveCell.EntireRow.Insert 'Insert row
End If

Thank you for your suggestion David, however the values/number of rows to insert in Column A and the identifiers in Column B will be changing, so I cannot hard code a starting cell or the number of rows to insert.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,130
Members
453,021
Latest member
Justyna P

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