Search code examples
vbaexcelvariablesoffset

Excel VBA. How to select a certain cell, then offset a variable number or rows down, and then print that selection


Just like it says in the title. I need to print in an area from B18 to J18 and down a variable number of rows. I don't know how to set this up. That variable number is being kept in cell O24 by using a =COUNTA(B20:B65536)function. I just need the selection to go down that many number of rows.

Here is my code so far.

Sub PrintPlease()

I = Cells("O24").Value

With ActiveSheet.PageSetup
    .Zoom = False
    .Orientation = xlPortrait
    .FitToPagesWide = 1
    .FitToPagesTall = 1

    ExecuteExcel4Macro ("PAGE.SETUP(,,,,,,,,,,,,{#N/A,#N/A})")
    If .Zoom < 30 Then
        .Zoom = 50
    Else
        .Zoom = False
        .FitToPagesWide = 1
    End If

End With

Range ("B18:J18"), Cells("B18").Offset((I), 3).PrintOut, Preview:=True

End Sub

Solution

  • You can use

    Range("B18:J" & Range("O24").Value).PrintOut Preview:=True
    

    Since the value in cell O24 contains the number of rows you want to print down to you can print the range beginning at cell B18 and ending at cell J and the value of cell O24 by appending the value in cell O24 to B18:J.