How to set variable range with some cells in columns blank?

Jeddo

Board Regular
Joined
Jan 26, 2019
Messages
50
Office Version
  1. 2019
Platform
  1. Windows
I am trying to set a variable print range for retrieved data in which some of the cells may be blank. The range could be from one row up to 250 rows depending on the data retrieved. Columns A & B will not have any blank cells, but columns beyond C & D may have blank cells. As in the workbox below, I would need to set a print range of A1 to D17. I have a formula that will find the last row in column B, but I can't figure out how to set the range for two more columns to the right.

Range("A1", Range("B1").End(xlDown)).PrintOut

How do I set the print range to encompass columns C & D?

Print template.xlsx
ABCD
1Sold By: Me
2
3NameBusiness NamePhone #
41John 555-1234
52James
63Paul P&S Cattle Co.555-1236
74Henry555-1237
85George
96SusanAnytown Auto Parts555-1239
107Ginny555-1240
118EdithEdith's Closet555-1241
129Beatrice555-1242
1310Eric
1411Joe555-1244
1512ShawnShawn's Landscaping555-1245
1613Steven
1714Bobby
Sheet1
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
VBA Code:
Range("A1:D" & Range("B1").End(xlDown).row).PrintOut
I can get the code to work for the example range it is set to, but when I try to modify it for the ranges I need, it doesn't work. I thought it would just be a matter of changing the columns in the formula, but it won't work.

Current range I am trying to set it up for is AZ6 to BC61. Changed the code to the following: Range("AZ6:BC" & Range("BA8").End(xlDown).row).PrintOut It prints the range AZ1 to BC8 instead of AZ6 to BC61, going UP from BA8 instead of down from BA8.

I'm missing something in the code when I try to set up a different range then the example I sent. I have several ranges like this I am trying to set up, so if I can get one to work and understand the syntax, I can get the rest. Any ideas?
 
Upvote 0
VBA Code:
debug.print Range("BA8").End(xlDown).row
what result is in immediate window? is it 61?
 
Upvote 0
@Jeddo - how are you determing what block of columns to test ?

If you are hard coding the columns in the code then you could use something like this:
VBA Code:
Sub LastRow()

    Dim sht As Worksheet
    Dim rng As Range
    
    Set sht = ActiveSheet                      ' <--- if not the activesheet put in the sheet
    Set rng = sht.Range("J:L")               ' <--- Change this to the desired columns
    
    Dim lastRow As Long, lastColumn As Long
    
    lastRow = rng.Find(What:="*" _
                , Lookat:=xlPart _
                , LookIn:=xlFormulas _
                , searchorder:=xlByRows _
                , searchdirection:=xlPrevious).Row
End Sub
 
Upvote 0
@Jeddo - how are you determing what block of columns to test ?

If you are hard coding the columns in the code then you could use something like this:
VBA Code:
Sub LastRow()

    Dim sht As Worksheet
    Dim rng As Range
   
    Set sht = ActiveSheet                      ' <--- if not the activesheet put in the sheet
    Set rng = sht.Range("J:L")               ' <--- Change this to the desired columns
   
    Dim lastRow As Long, lastColumn As Long
   
    lastRow = rng.Find(What:="*" _
                , Lookat:=xlPart _
                , LookIn:=xlFormulas _
                , searchorder:=xlByRows _
                , searchdirection:=xlPrevious).Row
End Sub
Please keep in mind I am a pathetic novice when it comes to VBA.

To your first question, I am hard coding the columns. I have several different ranges that collect data based upon what search I am doing. Each range has a specific set of columns, but only one of those columns is certain to have no blank cells between the data. The rows can run anywhere from 1 to 250. Didn't want to be printing out 6-7 pages of blank paper.

Tried your coding. I can get it to print the first two columns, with all of the desired data, but doesn't print any of the two columns to the right.
Changed the desired columns to "AZ:BC"
Changed Find(What:= to "BA6"
It printed out the desired rows of columns AZ and BA, but nothing for BB and BC
I was wanting it to print the range AZ6:BC44, but it printed AZ6: BA44
 
Upvote 0
Which is why I asked how you are determining the columns. The code I gave you only gets you the last row for the range of columns you tell it to use. You still need to tell the print area what range you want it to use.

Try something like this:
(I am sure someone can give you a more compact version but start with this)
VBA Code:
Sub testFindAndPrint()

    Dim sht As Worksheet
    Dim rng As Range
    Dim startCell As Range
    Dim lastRow As Long
    
    Set sht = ActiveSheet                      ' <--- if not the activesheet put in the sheet
    Set rng = sht.Range("AC:BC")               ' <--- Change this to the desired columns
    Set startCell = sht.Range("AC6")           ' <--- Change this to the desired top left corner of print range
    
    lastRow = rng.Find(What:="*" _
                , Lookat:=xlPart _
                , LookIn:=xlFormulas _
                , searchorder:=xlByRows _
                , searchdirection:=xlPrevious).Row

    startCell.Resize(lastRow - startCell.Row + 1, rng.Columns.Count).PrintOut
                
End Sub
 
Upvote 0
Solution
Which is why I asked how you are determining the columns. The code I gave you only gets you the last row for the range of columns you tell it to use. You still need to tell the print area what range you want it to use.

Try something like this:
(I am sure someone can give you a more compact version but start with this)
VBA Code:
Sub testFindAndPrint()

    Dim sht As Worksheet
    Dim rng As Range
    Dim startCell As Range
    Dim lastRow As Long
   
    Set sht = ActiveSheet                      ' <--- if not the activesheet put in the sheet
    Set rng = sht.Range("AC:BC")               ' <--- Change this to the desired columns
    Set startCell = sht.Range("AC6")           ' <--- Change this to the desired top left corner of print range
   
    lastRow = rng.Find(What:="*" _
                , Lookat:=xlPart _
                , LookIn:=xlFormulas _
                , searchorder:=xlByRows _
                , searchdirection:=xlPrevious).Row

    startCell.Resize(lastRow - startCell.Row + 1, rng.Columns.Count).PrintOut
               
End Sub
Getting an error message "Expecting End Sub". Didn't get this with your first code you sent me and can't figure out what the difference is that is causing it.
Here is my entire code, don't laugh too hard. Getting bits and pieces from google

VBA Code:
Private Sub cmdPrintSoldBy_Click()

If MsgBox("Do you want to print out Ticket Sold By List?", vbQuestion + vbYesNo) <> vbYes Then
Exit Sub
End If

Copy_Sold_By

With Sheet1
    Application.PrintCommunication = False
    With .PageSetup
    
        .BottomMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0#)
        .TopMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0#)
        .LeftMargin = Application.InchesToPoints(0.25)
        .RightMargin = Application.InchesToPoints(0.25)
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintArea = Sheet1.Range("AZ6:BC61").Address
        
    End With
    Application.PrintCommunication = True
   End With

Sub testFindAndPrint()

    Dim sht As Worksheet
    Dim rng As Range
    Dim startCell As Range
    Dim lastRow As Long
    
    Set sht = ActiveSheet                      ' <--- if not the activesheet put in the sheet
    Set rng = sht.Range("AZ:BC")               ' <--- Change this to the desired columns
    Set startCell = sht.Range("AZ6")           ' <--- Change this to the desired top left corner of print range
    
    lastRow = rng.Find(What:="BA6" _
                , Lookat:=xlPart _
                , LookIn:=xlFormulas _
                , searchorder:=xlByRows _
                , searchdirection:=xlPrevious).Row

    startCell.Resize(lastRow - startCell.Row + 1, rng.Columns.Count).PrintOut
                
End Sub

With Sheet1
    Application.PrintCommunication = False
    With .PageSetup
    
         .Zoom = 100
          
    End With
    Application.PrintCommunication = True
   End With

End Sub
 
Upvote 0
Since you are copying the code into an existing Sub, you need to get rid of these lines:
VBA Code:
Sub testFindAndPrint

and this line which is before the line, With Sheet1
VBA Code:
End Sub

(so not the one right at the end)
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,018
Members
449,203
Latest member
tungnmqn90

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