Macro to Border and Select Print Area for Queried Data

rickincanada

Board Regular
Joined
Aug 31, 2010
Messages
61
I have a workbook that queries a SQL database and returns a different number of records each time. The query populates data from columns A thorugh J, however I'd also like to have an additional blank column print as well. My challenge is that I want to include this additional column, in this case K, when adding a border and setting my print area via a macro - can anyone offer me an idea of how to do this?

Thanks so much!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Something like this?

Code:
Public Sub PrintAreaSet()
Dim LR  As Long, _
    LC  As Long
    
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column + 1
With Range(Cells(1, 1), Cells(LR, LC))
    .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=xlColorIndexAutomatic
    .PageSetup.PrintArea = .Address
End With
End Sub
 
Upvote 0
This only outlines as far as J and I'm getting the following error:

Run-time error '438'
Object doesn't support this proprerty or method

The item it highlights in debug is .PageSetup.PrintArea = .Address

Excel 2003. Thanks!
 
Upvote 0
Doh - that'll teach me for not testing code before handing it out... minor error:

Code:
Public Sub PrintAreaSet()
Dim LR  As Long, _
    LC  As Long
    
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column + 1
With Range(Cells(1, 1), Cells(LR, LC))
    .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=xlColorIndexAutomatic
    [B][COLOR=red]ActiveSheet[/COLOR][/B].PageSetup.PrintArea = .Address
End With
End Sub
 
Upvote 0
Do you have data going all the way out to column J in row 1 in your spreadsheet? It works fine on my end.
 
Upvote 0
I see... in that case, try:

Code:
Public Sub PrintAreaSet()
Dim LR  As Long, _
    LC  As Long
    
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells([B][COLOR=red]2[/COLOR][/B], Columns.Count).End(xlToLeft).Column + 1
With Range(Cells(1, 1), Cells(LR, LC))
    .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=xlColorIndexAutomatic
    [COLOR=black]ActiveSheet[/COLOR].PageSetup.PrintArea = .Address
End With
End Sub

Basically, it is looking for the last column in the row highlighted in red in my above code. You may need to adjust this number to a row that always has data in it if you have multiple blank rows above your data.
 
Upvote 0
That's perfect - the only other thing though is that I want to border all fields entirely, not just around. I know - this must be painful...

Thank you!
 
Upvote 0
I know - this must be painful...

Not painful at all! You gave a good thread title and a short, but detailed, description. Those kinds of posts are always a sight for sore eyes! :biggrin:

Code:
Public Sub PrintAreaSet()
Dim LR  As Long, _
    LC  As Long
    
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column + 1
With Range(Cells(1, 1), Cells(LR, LC))
    .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=xlColorIndexAutomatic
    With .Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    ActiveSheet.PageSetup.PrintArea = .Address
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,673
Members
449,248
Latest member
wayneho98

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