Macro or formatting to always show one extra row in a section

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi,

I have the following sample data set that could change project by project. Is there a way where I can add 50 extra rows into each section but only have it automatically show 1 extra row in the section so it's not overly long to scroll through and print?

Projects constantly change and evolve and would like to make the spreadsheet capable of growing with the projects.

200618 - Lambert - June Billing Hours.xlsx
BC
1ItemDescription
2001Main driveway curbing, not to exceed amount
3002Type 1 - Flagstone cladding and cap, not to exceed amount
4003Type 2 - Flagstone cladding and cap, not to exceed amount
5004Type 3 - Flagstone cladding and cap, not to exceed amount
6Site, Extra 01 Subtotal:
7005Laundry Room Tile Floor, Not to exceed amount
8006Bathroom 006, Not to exceed amount
9007Reserve Closet 005, Not to exceed amount
10008Circulation 007 Tile Flooring, Not to exceed amount
11009Circulation 106, Powder 103 Tile Flooring, Not to exceed amount
12010Kitchen 201, Breakfast 202, Living 203, Dining 204, Pantry 205 Tile Floor, Not to exceed amount
13011Circulation 208 Stairs – nosing, tread, riser, Not to exceed amount
14012Living Room 203 Fireplace Facing and Mantle, Not to exceed amount
15013Circulation 208, Powder 206, Mud 207, Hall R245 Tile Flooring, Not to exceed amount
16014Master Bath 302 Flooring, Not to exceed amount
17015Shower Wall & Floor, Not to exceed amount
18016Wall Cladding & Base at 301A and 302B, Not to exceed amount
19017Bath 307 Tile Flooring, Not to exceed amount
20018Bath 307 Shower Tile, Not to exceed amount
21019Bath 310 Tile Flooring, Not to exceed amount
22020Bath 310 Shower Tile, Not to exceed amount
23021Circulation 106 Stair Treads and Risers, Not to exceed amount
24022Circulation 006 Stair Treads and Risers
25023Main Stair Treads, Risers, and Landings, Not to exceed amount
26024Yoga Room Countertop, Not to exceed amount
27025Reserve Closet 005 Corian Countertop, Not to exceed amount
28026Powder Room 103 Corian Countertop, Not to exceed amount
29027Kitchen 201 Counters, Not to exceed amount
30Kitchen Table Top & End Waterfall
31028Powder 206 - Countertop, Not to exceed amount
32029Mud Room 207 – Countertop and splash, Not to exceed amount
33030Pantry 205 – Countertop and splash, Not to exceed amount
34031Lisa’s Closet 301 – Slab Island Top, Not to exceed amount
35032Lisa’s Closet 301 – Perimeter Counters, Not to exceed amount
36033Master Bath 302 Vanity Top, Not to exceed amount
37034Master Bath 302 Water Closet Counters, Not to exceed amount
38035Master Bath 302 Tub Deck & Risers, Not to exceed amount
39036Charles Closet 304 Countertop, Not to exceed amount
40037Bath 307 Vanity Top, Not to exceed amount
41038Bath 310 Vanity Top, Not to exceed amount
42039Reserve Closet 311 Countertops, Not to exceed amount
43Master Bedroom Wall Cladding, added in Bulletin 12
44Addition, Extra 02 Subtotal:
NTE Tracking Log
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
add 50 extra rows into each section
Assuming that a 'section' means those rows between the empty cells in column C, then try this with a copy of your workbook.

VBA Code:
Sub Insert_Rows()
  Dim cell As Range
  
  Const RowsToAdd As Long = 50
  
  Application.ScreenUpdating = False
  For Each cell In Range("C2:C" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks)
    cell.EntireRow.Resize(RowsToAdd).Insert
    cell.Offset(-RowsToAdd).EntireRow.Resize(RowsToAdd - 1).Hidden = True
  Next cell
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Peter,

Thank you very much for the response. I actually didn't explain that very well and the macro almost did it perfectly however. Below is a sample of what was produced and hopefully clarifying info as to what the goal is ?

Book1
ABC
1ItemDescription
2001Main driveway curbing, not to exceed amount
3002Type 1 - Flagstone cladding and cap, not to exceed amount
4003Type 2 - Flagstone cladding and cap, not to exceed amount
5004Type 3 - Flagstone cladding and cap, not to exceed amount
55
Sheet1


I actually didn't mean for the macro to add the rows in, itself. I meant to say if I have already expanded the sections (let's say to 100 rows), is there a way to only show lines 2-6 vs the version above showing lines 2-5 and then line 55. The reason I'd like to show 6 is so that if our project managers need to add to the scope of work, they can start typing it into line 6 and if line 6 then has data in either columns b or c then line 7 will show and so forth. Also if line 6 were cleared out (B and/or C being the criteria that governs whether the next blank line is shown) than it would go back to showing lines 2-6 vs 2-7. Basically the macro shows the next blank line in a section and could expand or contract to however big the scope of work is. Hopefully that makes a little more sense (only showing a few extra rows in the example below).

Book2
BC
1ItemDescription
2001Main driveway curbing, not to exceed amount
3002Type 1 - Flagstone cladding and cap, not to exceed amount
4003Type 2 - Flagstone cladding and cap, not to exceed amount
5004Type 3 - Flagstone cladding and cap, not to exceed amount
6
7
8
9
10
11Site, Extra 01 Subtotal:
12005Laundry Room Tile Floor, Not to exceed amount
13006Bathroom 006, Not to exceed amount
14007Reserve Closet 005, Not to exceed amount
15008Circulation 007 Tile Flooring, Not to exceed amount
16009Circulation 106, Powder 103 Tile Flooring, Not to exceed amount
17010Kitchen 201, Breakfast 202, Living 203, Dining 204, Pantry 205 Tile Floor, Not to exceed amount
18011Circulation 208 Stairs – nosing, tread, riser, Not to exceed amount
19012Living Room 203 Fireplace Facing and Mantle, Not to exceed amount
20013Circulation 208, Powder 206, Mud 207, Hall R245 Tile Flooring, Not to exceed amount
21014Master Bath 302 Flooring, Not to exceed amount
22015Shower Wall & Floor, Not to exceed amount
23016Wall Cladding & Base at 301A and 302B, Not to exceed amount
24017Bath 307 Tile Flooring, Not to exceed amount
25018Bath 307 Shower Tile, Not to exceed amount
26019Bath 310 Tile Flooring, Not to exceed amount
27020Bath 310 Shower Tile, Not to exceed amount
28021Circulation 106 Stair Treads and Risers, Not to exceed amount
29022Circulation 006 Stair Treads and Risers
30023Main Stair Treads, Risers, and Landings, Not to exceed amount
31024Yoga Room Countertop, Not to exceed amount
32025Reserve Closet 005 Corian Countertop, Not to exceed amount
33026Powder Room 103 Corian Countertop, Not to exceed amount
34027Kitchen 201 Counters, Not to exceed amount
35Kitchen Table Top & End Waterfall
36028Powder 206 - Countertop, Not to exceed amount
37029Mud Room 207 – Countertop and splash, Not to exceed amount
38030Pantry 205 – Countertop and splash, Not to exceed amount
39031Lisa’s Closet 301 – Slab Island Top, Not to exceed amount
40032Lisa’s Closet 301 – Perimeter Counters, Not to exceed amount
41033Master Bath 302 Vanity Top, Not to exceed amount
42034Master Bath 302 Water Closet Counters, Not to exceed amount
43035Master Bath 302 Tub Deck & Risers, Not to exceed amount
44036Charles Closet 304 Countertop, Not to exceed amount
45037Bath 307 Vanity Top, Not to exceed amount
46038Bath 310 Vanity Top, Not to exceed amount
47039Reserve Closet 311 Countertops, Not to exceed amount
48Master Bedroom Wall Cladding, added in Bulletin 12
49
50
51
52
53
54Addition, Extra 02 Subtotal:
55040Pool House Floor Tile, Not to exceed amount
Sheet1
 
Upvote 0
Hmm, I'm not sure that I have understood that fully but try this Worksheet_Change code with a copy of your workbook. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.
4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim LastRow As Long
  
  Set Changed = Intersect(Target, Columns("B:C"))
  If Not Changed Is Nothing Then
    Application.ScreenUpdating = False
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    For Each c In Changed
      If IsEmpty(Cells(c.Row + 1, "B").Value) And IsEmpty(Cells(c.Row + 1, "C").Value) And c.Row < LastRow Then
        With Range(Cells(c.Row + 1, "B").End(xlUp), Cells(c.Row, "B").End(xlDown))
          .EntireRow.Hidden = False
          If .Rows.Count > 4 Then .Offset(2).Resize(.Rows.Count - 4).EntireRow.Hidden = True
        End With
      End If
    Next c
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
When I insert the code I get the attached error message.

I'll try to explain further what the end goal is. Original data would look like this without any macro:

SVAR Actual - Budget v2.xlsm
ABCDEF
1COR-PCO Number:
2
3Change Order:
4
5RFI/Bulletin/ROM:
6
7Status:
8
9Item #DescriptionMaterial Unit PriceShop LaborSite LaborTotal
10001Main driveway curbing, not to exceed amount----
11002Type 1 - Flagstone cladding and cap, not to exceed amount----
12003Type 2 - Flagstone cladding and cap, not to exceed amount----
13004Type 3 - Flagstone cladding and cap, not to exceed amount----
14005Test----
15
16
17
18
19
20
21
22
23
24
25
26
27Site, Extra 01 Subtotal:----
Input
Cell Formulas
RangeFormula
F10:F14,F27F10=SUM(C10:E10)
Cells with Data Validation
CellAllowCriteria
D7:F7List=Lists!$Y$1:$Y$3


As you could imagine, multiple sections of this and a much more complex scope of work, there would be quite a bit to scroll through. What i'm hoping the macro would do is to only show items 001 - 004 plus one extra line (line 14 in this example hiding rows 15-26):

SVAR Actual - Budget v2.xlsm
ABCDEF
1COR-PCO Number:
2
3Change Order:
4
5RFI/Bulletin/ROM:
6
7Status:
8
9Item #DescriptionMaterial Unit PriceShop LaborSite LaborTotal
10001Main driveway curbing, not to exceed amount----
11002Type 1 - Flagstone cladding and cap, not to exceed amount----
12003Type 2 - Flagstone cladding and cap, not to exceed amount----
13004Type 3 - Flagstone cladding and cap, not to exceed amount----
14
27Site, Extra 01 Subtotal:----
Input
Cell Formulas
RangeFormula
F10:F13,F27F10=SUM(C10:E10)
Cells with Data Validation
CellAllowCriteria
D7:F7List=Lists!$Y$1:$Y$3


Also if the macro could account for any expansion or contraction of the project. If our project managers add data to line 14 then it automatically shows line 15 like so:

SVAR Actual - Budget v2.xlsm
ABCDEF
1COR-PCO Number:
2
3Change Order:
4
5RFI/Bulletin/ROM:
6
7Status:
8
9Item #DescriptionMaterial Unit PriceShop LaborSite LaborTotal
10001Main driveway curbing, not to exceed amount----
11002Type 1 - Flagstone cladding and cap, not to exceed amount----
12003Type 2 - Flagstone cladding and cap, not to exceed amount----
13004Type 3 - Flagstone cladding and cap, not to exceed amount----
14005Test----
15
27Site, Extra 01 Subtotal:----
Input
Cell Formulas
RangeFormula
F10:F14,F27F10=SUM(C10:E10)
Cells with Data Validation
CellAllowCriteria
D7:F7List=Lists!$Y$1:$Y$3


And vice versa, if they delete a line that it would revert back (if line 14 were cleared out then it would go back to the second example above)
 

Attachments

  • 3.JPG
    3.JPG
    17.9 KB · Views: 5
Upvote 0
Your error message indicates that you already have a Worksheet_Change code for that worksheet. Since only one is permitted, any code provided here would have to be amalgamated with your existing code and that could only be done successfully if we know what that existing code is. Can you post it?

Secondly, this layout is somewhat different to what was posted originally ..
- Those extra rows at the top of the sheet are new to us
- Item & Description appear to have moved from columns B:C to columns A:B

Is it correct that the code you are seeking only needs to apply from row 9 and down?

Given all that, please note that I am likely to be away from the forum for at least a week very soon. If not resolved before I leave, I will look back here on my return to see if somebody else has stepped in and solved it for you.
 
Upvote 0
Gotcha, I was building this off of another workbook that was complete and in good working order and will eliminate the other code as it is irrelevant to this new use and test again in the morning.

I apologize for the shift and new format as I am only beginning to put this new workbook together, but yes this new code will only be used from row 9 going forward.

fyi, I was looking to use this code on other sheets within the workbook as well. As this first sheet will take in change orders and updates in the budget of our projects, subsequent sheets will mirror the line items for reporting purposes and thus would need this macro so that there’s no unnecessary scrolling.

Regardless, thank you for taking the time in assisting with this project :)
 
Last edited:
Upvote 0
Peter,

This primarily worked! Minor tweak, if possible? Could it be coded where line 26 doesn't show? Right now this is what it's currently showing (it's also occurring further down the sheet)

SVAR Actual - Budget v2.xlsm
ABCDEF
1COR-PCO Number:
2
3Change Order:
4
5RFI/Bulletin/ROM:
6
7Status:
8
9Item #DescriptionMaterial Unit PriceShop LaborSite LaborTotal
10001Main driveway curbing, not to exceed amount----
11002Type 1 - Flagstone cladding and cap, not to exceed amount----
12003Type 2 - Flagstone cladding and cap, not to exceed amount----
13004Type 3 - Flagstone cladding and cap, not to exceed amount----
14005Test----
15
26
27Site, Extra 01 Subtotal:----
Input
Cell Formulas
RangeFormula
F10:F14,F27F10=SUM(C10:E10)
Cells with Data Validation
CellAllowCriteria
D7:F7List=Lists!$Y$1:$Y$3
 
Upvote 0
Try this version

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim FirstRow As Long, LastRow As Long

  Set Changed = Intersect(Target, Columns("A:B"), Rows("9:" & Rows.Count))
  If Not Changed Is Nothing Then
    Application.ScreenUpdating = False
    For Each c In Changed
      If IsEmpty(Cells(c.Row + 1, "A").Value) And IsEmpty(Cells(c.Row + 1, "B").Value) Then
        LastRow = Cells(c.Row + 1, "A").End(xlDown).Row
        FirstRow = Columns("A:B").Find(What:="*", After:=Cells(LastRow, "A"), SearchDirection:=xlPrevious).Row
        Rows(FirstRow & ":" & LastRow).Hidden = False
        If LastRow < Rows.Count And LastRow - FirstRow > 2 Then Rows(FirstRow + 2 & ":" & LastRow - 1).Hidden = True
      End If
    Next c
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Peter,

This works perfectly! Thank you very much for your time on this!

This will help manage our projects a lot more seamlessly and save one of our project managers an unreal amount of time of hand calculating etc.
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,956
Members
449,200
Latest member
indiansth

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