VBA Code Needed: Add New Row(s) to Multiple "Sections" in absolute location & copy formulas to new rows

fuhsball

New Member
Joined
Aug 28, 2013
Messages
7
General Info

I'm using Windows 7, Excel 2010

Model Background

I have a model that has various hard coded assumptions (volume, price, cost, etc...) and then various calculations (i.e. Volume * Price = Revenue, etc...). Each unique assumption or calculation is allowed to have up to 50 rows of input. These 50 rows make up a unique section within the model. Each unique “section” or cell range has 50 rows and 48 columns.

Examples of “Sections” as found on Original worksheet (attached file)

  • Volume Section = A2:AV51 (FYI, I currently have a blank row between each section)
  • Price Section = A53:AV102
  • Revenue Calc Section = A104:AV153

The very first section is the master section (volume in the attached file) and this is where the user will define (or hard code) the data found in columns A – U (i.e. A2:U51). Each of the other sections found below the master all link into the master section essentially replicating the descriptor values found in the master section to all other sections. You’ll also notice that Column A has the values 1-50 and this is the same in all sections. The idea is that all the #1’s in column A are tied together for all descriptors and then also the hard coded assumptions and formulas found in columns V – AV.

Needed Functionality

A user will be using this model and have data populated for values 1-20 (column A) for all sections. The user will then get additional information that will require them to refine the model. For clarify the attached file goes through single scenario in which the user wants to add 3 new rows below #10 (in column A) for all sections (volume, price, revenue).

In the attached file you'll find three worksheets.

  1. The Original worksheet shows an example of a model prior to making any changes.
  2. The Modified (inserted new rows) worksheet shows (in my mind) a step within the code in which the user states they want to insert 3 new rows below #10 (in column A) in all sections within the model (recalling each section has 50 rows and is labeled with 1-50 in column A). So when this step is completed each section will have 53 rows instead of 50.
  3. The Modified (New Rows Populated) worksheet is trying to show how I need to copy formulas, update column A to have the correct sequenced #’s from 1-53, and then copy the formulas found in columns V-AV for the three newly created rows. Anything highlighted in yellow in my mind has to be updated after the rows are inserted. The red highlight in the Master Section (Volume) will require user input. Once they input the data it will filter into the sections below.

So in summary I’m looking code that looks in column A and finds value = 10 (value specified by the user) in section 1 and then adds a user specified # of new rows below value #10, then it would go to section 2 and find value 10 and add the same amount of user specified rows below value 10 in that section and continue to loop through all sections found in the model to add the new rows to all sections in the same place within each section. Then if possible updated the values in column A so the numbers go from 1-53 or 1-58 if the user says they wanted to add 8 rows instead of 3. Then lastly copy and paste the formulas found in row 10 to the newly created rows below #10.

If there’s a real easy way to complete what I’m asking please just let me know. This can be done manually but is very time consuming and user acceptance of the manual approach is very low. I’ve searched the forum and the net and found code that lets you add a specified row at various points (i.e. every 10th row) but nothing that quite does exactly what I'm trying to do. Then I've also found a basic idea on trying to use sorting to accomplish this task but so far I haven't found a solution that works.

Thanks in advance for any help. Any questions let me know.

Hmm...apparently you can't post attachments. Well hopefully the above details are detailed enough to understand what the model looks like and what I’m trying to do. I’ll dig around a little more to see how I can get my example file more visible to everyone. Any advice on this let me know.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
After thinking a little more on my original post I have a feeling it's going to be hard to follow and provide any type of solution without the actual excel file being available or an example to look at so I felt it necessary to reply to my original post with the below update. Hopefully, this is ok I did spend a little time reviewing the forum rules prior to replying to my own post...but if someone does have time to help me on this I didn't want them to waste time due to the problem / desired solution not being clear.

The goal is to insert "X" rows (ideally specificed by the user...macro would prompt the user for input) below the identified value in Column A. So a user would say I want to insert 1 row every time the value of 2 is found in column A. You'll notice that in each of the below sections in Column A #1 is always related to the same Program 1 in all three sections of the file.

Section 1
ABCD
1#ProgramDescriptionFormula / Input
21Program 1Volume100
32Program 2Volume200
43Program 3Volume300
54Program 4Volume400

<tbody>
</tbody>

Section 2
ABCD
7#ProgramDescriptionFormula / Input
81Program 1Price$500
92Program 2Price$500
103Program 3Price$500
114Program 4Price$500

<tbody>
</tbody>

ABCD
13#ProgramDescriptionFormula / Input
141Program 1Revenue=D2*D8
152Program 2Revenue=D3*D9
163Program 3Revenue=D4*D10
174Program 4Revenue=D5*D11

<tbody>
</tbody>

While continuing to dig through the forums I did find the below code which allows me insert a row as outlined above...it does not have the user prompting cabability that I would like so if anyone knows how to modify the below code to include user prompting of the value to search for in Column A and then the # of blank rows to insert below the found value I'd appreciate the help on that. The below code works well to insert a single row (and playing around with it you can change the Find value from "2" to "X" and change the rows to insert from "1" to "X" if needed manually. The only think I didn't like about the below solution is that it took a little while to run through all 30K lines of data that I have...but that may be unavoidable with so much data.



Sub Seas2K()

Dim c As Range
Dim firstaddress As String
Dim firstaddress2 As String

With ActiveSheet.Range("A:A")
Set c = .Find("2", LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
firstaddress2 = c.Offset(1, 0).Address
Do
.Range(Cells(c.Row + 1, 1).Address, Cells(c.Row + 1, 1).Address).EntireRow.Insert
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstaddress And c.Address <> firstaddress2
End If
End With

Set c = Nothing

End Sub



After I run the above code I end up with data that looks like the below. It correctly goes into each "Section" of the worksheet finds a value of "2" in column A and inserts a blank row below the row where the value of "2" was found.

Section 1 (after macro)
ABCD
1#Program DescriptionInput/Formula
21Progam 1Volume100
32Program 2Volume200
4
53Program 3Volume300
64Program 4Volume400

<tbody>
</tbody>

Section 2 (after macro)
ABCD
8#Program DescriptionInput/Formula
91Progam 1Price$500
102Program 2Price$500
11
123Program 3Price$500
134Program 4Price$500

<tbody>
</tbody>

Section 3 (after macro)
ABCD
15#Program DescriptionInput/Formula
161Progam 1Revenue
172Program 2Revenue
18
193Program 3Revenue
204Program 4Revenue

<tbody>
</tbody>


So what I'm still looking for is a way to copy the data/formulas from the row above the newly inserted row and do a paste formulas in the newly created row (i.e. copy row 3 & paste formula into row 4, copy row 10 & paste special formulas in row 11, and so on down the spreadsheet until all newly created rows are filled in). Also, the newly blank rows create a numbering issue within Column A. So in section 1 Cell A4 is now blank but it needs to equal 3 and then likewise all numbers below A4 need to be updated to reflect the newly inserted row.

Another thought I've had is that you could just copy the row where the specified value of "2" is found and insert the copied values below the existing row. Just another way to do it I guess.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,964
Members
449,276
Latest member
surendra75

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