Format only columns with values

BDexcel

New Member
Joined
Jun 28, 2017
Messages
44
Hi all,

Could someone help me to amend the below code?

I use the below code on as part of copying varies results to a new tab and applying a border. Some of these extracts have 5 columns some have 10 so i used A2:Z... but obviously this borders all cells from A:Z.

I want the code to only border columns as far as the last one with values, could someone help?

lr = Cells(Columns.Count, "A").End(xlUp).Row
Range("A2:Z" & lr).Select
Selection.EntireColumn.AutoFit
With Selection.Borders
.LineStyle = xlContinuous
.Color = vbBlack
.Weight = xlThin
End With
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Re: Help to format only columns with values

Try:
Code:
Sub addBorders()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    With Range(Cells(2, 1), Cells(LastRow, lCol))
        .Columns.AutoFit
        .Borders.LineStyle = xlContinuous
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Re: Help to format only columns with values

unfortunately still doing the same mumps :( but appreciate the reply
 
Upvote 0
Re: Help to format only columns with values

I tried the macro on a dummy sheet and it worked properly. Do you want the borders on the new sheet after you paste the data? If so, what is the name of that new sheet?
 
Upvote 0
Re: Help to format only columns with values

yeah, sorry mumps... My code filters a main sheet and pastes into a new 'sheet1'

The only part i'm having bother with is bordering the new data as the extracted data can vary in size
 
Upvote 0
Re: Help to format only columns with values

Try:
Code:
Sub addBorders()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lCol As Long
    lCol = Sheets("Sheet1").UsedRange.Columns.Count
    With Sheets("Sheet1").Range(Sheets("Sheet1").Cells(2, 1), Sheets("Sheet1").Cells(LastRow, lCol))
        .Columns.AutoFit
        .Borders.LineStyle = xlContinuous
    End With
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Re: Help to format only columns with values

You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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