Always Print Column B

IREALLYambatman

Board Regular
Joined
Aug 31, 2016
Messages
63
Hi guys,
I am trying to write a column where if I have 25 columns of data.. I want it to print every 7-8 columns, but have column B always be one of those 7-8 columns, this is what I came up with but I don't know how to add the column B to the macro.


VBA Code:
Sub PrintEvery6Columns()
    Dim startCol As Integer
    Dim endCol As Integer
    Dim i As Integer

    startCol = 2
    endCol = startCol + 5
  
    ' Find the total number of columns
    Dim lastCol As Long
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
  
    ' Loop through the columns, printing every 6 columns as a new page
    Do While endCol <= lastCol
        For i = startCol To endCol
            Range(Columns(i).Address & ":" & Columns(i).Address).Select
        Next i
        ActiveSheet.PrintOut
        startCol = endCol + 1
        endCol = startCol + 5
    Loop
    If endCol > lastCol Then
        Range(Columns(startCol).Address & ":" & Columns(lastCol).Address).Select
        ActiveSheet.PrintOut
    End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The answer to my question is to combine ranges with Union, example below for anyone who might need it. Thanks ChatGPT lol.
VBA Code:
Sub PrintColumns()
    Dim ws As Worksheet
    Dim lastCol As Long
    Dim printRange As Range
    Set ws = ActiveSheet

    'Find the last column with data in row 2
    lastCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column

    'Print every 6 columns starting from column B until the last column
    For i = 2 To lastCol Step 6
        'Combine the range for columns and column B
        If printRange Is Nothing Then
            Set printRange = Union(ws.Range(ws.Cells(1, i), ws.Cells(ws.Rows.Count, i + 5)), ws.Range("B:B"))
        Else
            Set printRange = Union(printRange, ws.Range(ws.Cells(1, i), ws.Cells(ws.Rows.Count, i + 5)), ws.Range("B:B"))
        End If
    Next i
    printRange.PrintOut
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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