I need help with a VBA Macro code to prevent cell border changes to unprotected cells

agross

New Member
Joined
Aug 3, 2012
Messages
3
I have unprotected cells in excel where I need to change the content (number) and cell color, but I don't want the cell borders to change. I'm a beginner with vba. I've tried protect sheet and workbook options, but looks like I need a vba code and don't know where to start. Audrey.
Win 7
Office 2007
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This would be one way. Change your range to suite.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range
Set r = Range("J6:L19")
    With r.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End Sub
 
Last edited:
Upvote 0
Thank you for your quick reply. I am getting an error on this line when I copy and paste into one of the unprotected cells: .LineStyle = xlContinuous

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range
Set r = Range("A3:H55")
With r.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
 
Upvote 0
Ive tested on my end and everything seems to be working just fine. Not that it caused any problems when i tried it, but do you have anything weird going on in that range like merged cells or anything like that?
 
Upvote 0
I don't have any merged cells, I have locked cells and formulas. . . I thought it was weird that it didn't like the .LineStyle = xlContinuous
Can you tell me what that means, or what it pertains to? I might find out what the problem is there.
 
Upvote 0

Forum statistics

Threads
1,216,079
Messages
6,128,687
Members
449,464
Latest member
againofsoul

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