Vba code to give borders to cell

shinumon

New Member
Joined
Sep 24, 2015
Messages
16
I would like to have a vba code so as to add borders from active cell to left till first column and upwards till first row.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
row() gives row number and column() gives column number so just move left , providing column number is greater than 1 and then when column number = 1 move up using same logic. are you reasonably familiar with macro code ?
 
Upvote 0
Hi
Is this what you're after
Code:
Sub AddBorder()
' shinumon

    With Range("A1", ActiveCell)
        .BorderAround LineStyle:=xlContinuous, Weight:=xlThin
        With .Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    End With

End Sub
 
Upvote 0
Possibly just...
Code:
Range("A1", ActiveCell).Borders.LineStyle:=xlContinuous
 
Upvote 0
Hello Mark
I'm getting a Compile Error - Expected expression & highlights the := but if I remove the : it works
 
Upvote 0
That's the trouble with posting code when you are only on a phone:biggrin:
 
Upvote 0
@shinumon
Use this modified version of Mark858' code
Code:
Range("A1", ActiveCell).Borders.LineStyle = xlContinuous
 
Upvote 0
Just to point out that with the code I posted (with the kind correction by Fluff) that it only works with 1 specific criteria and the rest set to the defaults i.e. you could do

Code:
Range("A1", ActiveCell).Borders.LineStyle = xlContinuous
or
Code:
Range("A1", ActiveCell).Borders.LineStyle = xlDash
or
Code:
Range("A1", ActiveCell).Borders.ColorIndex = 3
or
Code:
Range("A1", ActiveCell).Borders.Weight = xlThick

or any other single specified criteria.

but if for instance you wanted a bolded border that was a dash then you would have to use code similar to post number 4.

BTW, the code I posted (and was corrected) only does both outside and inside borders and not diagonals.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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