New table if count is larger than 20

mc136355

New Member
Joined
Mar 20, 2018
Messages
36
Hi

I have a spreadsheet that uses code to insert rows into my table based on a cell value. My problem is the cell value can be over 100 and i would like to somehow break the table up if value is 20. So if value is 20 then start a new table. This is purely for printing purposes (header on each printed page, followed by 20 rows of data(with subtotal if possible)) so if anyone has a better idea instead of creating a new table then i would appreciate your idea. Thanks MC
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi have gathered the following code:

Sub insertbreak()

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("Table1")
If tbl.DataBodyRange.Rows.Count <= 20 Then
MsgBox "do nothing"
Else
MsgBox "insert break"
End If
End Sub

i think i would manage to insert the page break but could you advise on how to insert subtotal after the 20 rows.
Thanks MC
 
Last edited:
Upvote 0
Try this :
Assuming the table start at colomn A & header at row 1.
First, you need to manually add the subtotal formula in row after the last row of the table.
I'm using loop to show every 20 row and hide the rest.
You can check by inserting a break point in line with ‘k = i + 1’ then hit F5 repeatedly, see what happen.
But I'm not sure about how to do the print part, I’m using this method (but for the moment I commented the lines):
' .Range.Select
' ActiveWindow.RangeSelection.PrintOut


Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1107921a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1107921-new-table-if-count-larger-than-20-a.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], k [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], rx [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], rc [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], j [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]With[/COLOR] ActiveSheet.ListObjects([COLOR=brown]"Table1"[/COLOR])
    rx = .Range.Rows.count
    k = [COLOR=crimson]2[/COLOR]
    [COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]To[/COLOR] rx - [COLOR=crimson]1[/COLOR]
        j = i - [COLOR=crimson]1[/COLOR]
        [COLOR=Royalblue]If[/COLOR] j [COLOR=Royalblue]Mod[/COLOR] [COLOR=crimson]20[/COLOR] = [COLOR=crimson]0[/COLOR] [COLOR=Royalblue]Or[/COLOR] i = rx - [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        Range([COLOR=brown]"A2:A"[/COLOR] & rx - [COLOR=crimson]1[/COLOR]).EntireRow.Hidden = [COLOR=Royalblue]True[/COLOR]
        Range([COLOR=brown]"A"[/COLOR] & k & [COLOR=brown]":B"[/COLOR] & i).EntireRow.Hidden = [COLOR=Royalblue]False[/COLOR]
        k = i + [COLOR=crimson]1[/COLOR]
            [I][COLOR=seagreen]'DO THE PRINT JOB HERE[/COLOR][/I]
            [I][COLOR=seagreen]'.Range.Select[/COLOR][/I]
            [I][COLOR=seagreen]'ActiveWindow.RangeSelection.PrintOut[/COLOR][/I]
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
    [COLOR=Royalblue]Next[/COLOR]
    
    .Range.EntireRow.Hidden = [COLOR=Royalblue]False[/COLOR]
[I][COLOR=seagreen]'    MsgBox "It's done"[/COLOR][/I]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
This one is more flexible than the code I gave you above.
The table doesn't need to start at col A & the header doesn't need to be at row 1.

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1107921b()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1107921-new-table-if-count-larger-than-20-a.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], k [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], rx [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], x [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], j [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]With[/COLOR] ActiveSheet.ListObjects([COLOR=brown]"Table1"[/COLOR]).DataBodyRange
    
    rx = .Rows.count
    x = [COLOR=crimson]20[/COLOR] [I][COLOR=seagreen]'limit every how many rows[/COLOR][/I]
    k = [COLOR=crimson]1[/COLOR]
    [COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] rx
        [COLOR=Royalblue]If[/COLOR] i [COLOR=Royalblue]Mod[/COLOR] x = [COLOR=crimson]0[/COLOR] [COLOR=Royalblue]Or[/COLOR] i = rx [COLOR=Royalblue]Then[/COLOR]
            .EntireRow.Hidden = [COLOR=Royalblue]True[/COLOR]
            .Cells(k, [COLOR=crimson]1[/COLOR]).Resize(x).EntireRow.Hidden = [COLOR=Royalblue]False[/COLOR]
            k = i + [COLOR=crimson]1[/COLOR]
                [I][COLOR=seagreen]'DO THE PRINT JOB HERE[/COLOR][/I]
                [I][COLOR=seagreen]'.Range.Select[/COLOR][/I]
                [I][COLOR=seagreen]'ActiveWindow.RangeSelection.PrintOut[/COLOR][/I]
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
    [COLOR=Royalblue]Next[/COLOR]
    
    .EntireRow.Hidden = [COLOR=Royalblue]False[/COLOR]
[I][COLOR=seagreen]'    MsgBox "It's done"[/COLOR][/I]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0

Forum statistics

Threads
1,213,515
Messages
6,114,080
Members
448,548
Latest member
harryls

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