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
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

mc136355

New Member
Joined
Mar 20, 2018
Messages
36
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:

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,571
Office Version
365
Platform
Windows
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]
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,571
Office Version
365
Platform
Windows
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]
 

Watch MrExcel Video

Forum statistics

Threads
1,102,050
Messages
5,484,417
Members
407,438
Latest member
DKrakken

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top