VBA code working on wrong worksheet

fvisions

Board Regular
Joined
Jul 29, 2008
Messages
191
Office Version
  1. 365
Platform
  1. Windows
This is working, sort of, it is doing what I want to the second tab in the workbook (CWS) BUT it is also doing the same steps to the first tab sheet 1(Columns) where my end user buttons are placed for my co-workers to use. How do I update this code to only work on the CWS tab?

VBA Code:
Sub Hide_Cells()

'Dim ws As Worksheet
Worksheets("CWS").Activate

Range("B1").Select
ActiveCell.FormulaR1C1 = "."
Range("D1").Select
ActiveCell.FormulaR1C1 = "."
Range("H1").Select
ActiveCell.FormulaR1C1 = "Current Equipment"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "."
Range("A1").Select

Columns("B:AC").Select
Selection.Locked = True
Selection.FormulaHidden = False
Columns("AD:AK").Select
Selection.Locked = False
Selection.FormulaHidden = False

For Each ws In ActiveWorkbook.Worksheets
ws.Range("B:f,H:j, L:O, Q:T").EntireColumn.Hidden = True
Next ws
ActiveSheet.Protect Password:="12121212"

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How about
VBA Code:
Sub Hide_Cells()

'Dim ws As Worksheet
With Worksheets("CWS")
   .Range("B1").Value = "."
   .Range("D1").Value = "."
   .Range("H1").Value = "Current Equipment"
   .Range("Q1").Value = "."
   .Range("A1").Select
   
   With .Columns("B:AC")
      .Locked = True
      .FormulaHidden = False
   End With
   With .Columns("AD:AK")
      .Locked = False
      .FormulaHidden = False
   End With
   .Protect Password:="12121212"
End With
For Each ws In ActiveWorkbook.Worksheets
ws.Range("B:f,H:j, L:O, Q:T").EntireColumn.Hidden = True
Next ws

End Sub
 
Upvote 0
Code still works on both tab instead of just the Worksheet("CWS"). This may be on me, I am not good at DIM - do I need it for this?
 
Upvote 0
Are you saying that the row 1 values are changing on sheets other than the CWS sheet?
 
Upvote 0
Yes, everything in the code is working twice, once in the CWS worksheet and the other in the worksheet Sheet 1. Sheet 1 is where the button with the assigned marco is located with a form control button and instructions for the end user. I have a second button for the end user to unlock the CWS worksheet as well.
 
Upvote 0
Re: "Code still works on both tab"
Do you mean the hiding of the Columns?
 
Upvote 0
With this part you tell it to work on all sheets in your workbook. Just delete these lines
Code:
For Each ws In ActiveWorkbook.Worksheets
ws.Range("B:F,H:J, L:O, Q:T").EntireColumn.Hidden = True
Next ws
And use this instead.
Code:
Worksheets("CWS").Range("B:f,H:j, L:O, Q:T").EntireColumn.Hidden = True
 
Upvote 0
With this part you tell it to work on all sheets in your workbook. Just delete these lines
Code:
For Each ws In ActiveWorkbook.Worksheets
ws.Range("B:F,H:J, L:O, Q:T").EntireColumn.Hidden = True
Next ws
And use this instead.
Code:
Worksheets("CWS").Range("B:f,H:j, L:O, Q:T").EntireColumn.Hidden = True
That works. Thank you so much for helping me.
 
Upvote 0
Glad you have it working and thanks for letting us know.
Good luck and stay safe
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

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