VBA Border

slivesay

Board Regular
Joined
Jan 4, 2019
Messages
64
I have the below coding and it works great, but I'd like add on to it. Right now it puts a border around all cells that contain value. My number of columns will always be the same (A-Y), but my rows will be different. How can I add the border to the blank cells right after my last column that contains data?


'BORDERS
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
How about
Code:
With ActiveSheet.UsedRange
   With .Resize(, .Columns.Count + [COLOR=#ff0000]1[/COLOR]).Borders
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = xlAutomatic
   End With
End With
change the value in red if you want more than 1 extra column
 
Upvote 0
you could try using row count on the longest column or whatever column you want.

so maybe
Code:
Sub test()
Dim lastRow As Long
Dim rng As Range

lastRow = Range("Y" & Rows.Count).End(xlUp).row
Set rng = Range("A1:Y" & lastRow)

rng.Select

With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End Sub

note: there cant be blank cells in Y

edit: Fluff strikes again- use theirs
 
Last edited:
Upvote 0
ah i didnt comprehend the +1 column

Code:
Sub test()
Dim lastRow As Long
Dim rng As Range

lastRow = Range("Y" & Rows.Count).End(xlUp).row
Set rng = Range("A1:Z" & lastRow)

rng.Select

With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End Sub
 
Upvote 0
@BlakeSkate.
The first part of you code is doing absolutely nothing ;)
You would need to change the code to look at the selection rather than the usedrange
 
Upvote 0
@BlakeSkate.
The first part of you code is doing absolutely nothing ;)
You would need to change the code to look at the selection rather than the usedrange

right...... well........i'll do a fixit later today :LOL:
gotta stay true to my signature somehow
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
I may need to do a new thread, but may I ask ---

Would the coding be hard if... In this same template to find the last row (since they vary) and total the numbers in one column? The 1st row is my headers, so J1 will be a word and the rest of the column will be numbers. Can I total the numbers and put the answer in the cell after the last row?
 
Upvote 0
Yes that can be done, but does need a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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