Option button macro code for two worksheets

ExcelNovice

Well-known Member
Joined
May 12, 2002
Messages
583
I'm using the following code to change the column width on two worksheets (Vehicles and CarsOnly), but while it's working on the Vehicles sheet, I'm getting an error on the CarsOnly sheet and I'm not sure what changes to make to the code. Everything in blue works as it should, but I'm getting error after that, and nothing in red works. Help please. Thanks.

Private Sub OptionButton1_Click()
ActiveSheet.Unprotect Password:="password1"
Range("OK63") = "200"
Range("ki42") = "200"
Sheets("CarsOnly").Range("ki42") = "200"

Range("bt42").Select
Columns("BR:EO").ColumnWidth = 0


Sheets("CarsOnly").Select
Columns("BR:EO").Select
Selection.ColumnWidth = 0
Range("BK45").Select
Sheets("Vehicles").Select



Range("BK45").Select


ActiveSheet.Protect Password:="password1"
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Are both sheets protected?
If so you are only unprotecting one of them
 
Upvote 0
Fluff, both sheets are protected, but with the same password so the unprotect/protect code should work on both sheets, and does.
 
Upvote 0
This
Code:
[COLOR=#0000ff]ActiveSheet[/COLOR].Unprotect Password:="password1"
only unprotects the Active sheet
 
Upvote 0
Thanks Fluff, you were correct.
See below (red) for the changes I made to fix the issue, and it worked, however instead of changing the width of columns BR:EO to zero on the CarsOnly sheet, it changed the width of all columns up to EP (that is, columns AA:EO) to zero.
I cannot figure out why the width of other columns other than BR:EO were changed to zero - any ideas? Thanks.


Range("OK63") = "200"
Range("ki42") = "200"
Sheets("CarsOnly").Range("ki42") = "200"

Range("bt42").Select
Columns("BR:EO").ColumnWidth = 0

Sheets("CarsOnly").Unprotect Password:="password1"
Sheets("CarsOnly").Select
Columns("BR:EO").Select
Selection.ColumnWidth = 0
Range("BK45").Select
Sheets("Vehicles").Select
Sheets("CarsOnly").Protect Password:="password1"
 
Upvote 0
Do you have any merged cells?
 
Upvote 0
What happens if you try this
Code:
Sheets("CarsOnly").Unprotect Password:="password1"
Sheets("CarsOnly").Columns("BR:EO").Hidden = True
Range("BK45").Select
Sheets("Vehicles").Select
Sheets("CarsOnly").Protect Password:="password1"
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
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