Vba to draw border on single column

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi,

Would anyone have any code to draw a border around the used range of a single column?

I have a vlookup which runs in a column adjacent to a PivotTable. I have code to recreate the border around the table each time it's refreshed but I need some code to also draw a border around this additional column.

Obviously this border is going to need to change dynamically as and when the table is refreshed.

Also, would any code need to go into the PivotTableupdate event or just a worksheet change event?

Cheers
 
so, unless cell D1 is filled, you code will produce a different result from RickXL's.

If matching RickXL's code then it would be

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const strCol As String = "D"    ' <-- Change column letter here

    Dim fr As Long
    Dim lr As Long

    fr = Cells(1, strCol).End(xlDown).Row
    lr = Cells(Rows.Count, strCol).End(xlUp).Row

    On Error Resume Next
    Range("D:D").Borders.LineStyle = xlNone
    Range(Cells(fr, strCol), Cells(lr, strCol)).Borders.Weight = xlThin
    On Error GoTo 0

End Sub

As an alternative to Rick Rothstein's

Code:
Sub XXX()
    Columns("D").Borders.LineStyle = xlNone
    On Error Resume Next
    Range(Columns("D").SpecialCells(xlConstants)(1), Cells(Rows.Count, "D").End(xlUp)).Borders.Weight = xlThin
    On Error GoTo 0
End Sub
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,215,375
Messages
6,124,583
Members
449,174
Latest member
chandan4057

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