Dynamic macro need to add/delete rows based on nonstatic 12 monthly values

holmes1013

New Member
Joined
Nov 17, 2011
Messages
2
Before we dive in, my knowledge of macros extends to the point where I can tweak already written codes to suit my needs. I've searched all over but this one is still throwing me for a loop (no pun intended).

I'm in Excel 2010, Win7.

I am trying to build a budget forecast based on revenue vs headcount. For each +/- $230k in revenue, that equates to +/- one employee.

Forecasted revenue numbers will be changing, which will then change the count of new temp employees needed for that month so the insert/delete needs to be flexible.

Range K14:V14 holds the incremental headcount per month needed. This range won't change.

Current employee data starts in A19 (A18 are headers), this won't change, and the end of this list is variable (I want the flexibility to add more FT employees and not mess up macro to adjust for temp help).

What I need the macro to do is:

1) Starting in A18, find first blank row searching downward. There is other data below where I want to start inserting so I can't search for blank from bottom of sheet.

2) Starting in that first blank row, based on Jan headcount value in K14, insert (shift rows downward) that many new rows. This January value will always be >=0.

3) For newly inserted rows, COPY THE FORMULA of row above for Ranges(B:E,G:V). The value in column A should always = "NEW". The value in column F should equal the first day of the month that we're looking at. In this case, F would = 1/1/2012.

4) Now look at Feb headcount value in L14.

5) If Feb value > 0, insert new rows. If Feb value < 0, delete that number of rows. If Feb=0, do nothing. For new inserts, again (and last time), Col F should = 2/1/2012.

*** If it's easier, any dates in ColF can reference the range K8:V8 for Jan-Dec date values.

6) Loop though above for rest of months through December.

THANK YOU IN ADVANCE FOR ANY HELP OR GUIDANCE!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> rngA <SPAN style="color:#00007F">As</SPAN> Range, n <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, MonthCell <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#007F00">' Loop though ("K14:V14") for months through December.</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> MonthCell <SPAN style="color:#00007F">In</SPAN> Range("K14:V14")<br>        n = MonthCell.Value<br>        <br>        <SPAN style="color:#007F00">' Starting in A18, find first blank row searching downward.</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rngA = Range("A18").End(xlDown)<br>        <br>        <SPAN style="color:#007F00">' If Month value > 0, insert new rows.</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> n > 0 <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#007F00">' Starting in that first blank row, based on headcount value,</SPAN><br>            <SPAN style="color:#007F00">' insert (shift rows downward) that many new rows.</SPAN><br>            rngA.Offset(1).Resize(n).EntireRow.Insert Shift:=xlShiftDown<br>            <SPAN style="color:#007F00">' For newly inserted rows, COPY THE FORMULA of row above for Ranges(B:E,G:V).</SPAN><br>            rngA.Resize(, 22).AutoFill Destination:=rngA.Resize(n + 1, 22)<br>            <SPAN style="color:#007F00">' The value in column A should always = "NEW".</SPAN><br>            rngA.Offset(1).Resize(n).Value = "New"<br>            <SPAN style="color:#007F00">' The value in column F should equal the first day of the month that we're looking at.</SPAN><br>            <SPAN style="color:#007F00">' dates in Col F can reference the range K8:V8 for Jan-Dec date values.</SPAN><br>            rngA.Offset(1, 5).Resize(n).Value = Cells(8, MonthCell.Column).Value<br>            <br>        <SPAN style="color:#007F00">' If value < 0, delete that number of rows.</SPAN><br>        <SPAN style="color:#00007F">ElseIf</SPAN> n < 0 <SPAN style="color:#00007F">Then</SPAN><br>            rngA.EntireRow.Offset(n + 1).Resize(Abs(n)).Delete<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <br>    <SPAN style="color:#00007F">Next</SPAN> MonthCell<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,618
Messages
6,125,870
Members
449,266
Latest member
davinroach

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