Setting all Excel sheets at a defined zoom level - excel

I have more than twenty sheets in an Excel workbook (file). Is there some code snippet or a command I could apply/use so that all sheets could be reset to let's say 85% zoom level?

Sub SetZoom()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
ActiveWindow.Zoom = 85 ' change as per your requirements
Next ws
End Sub
BTW, if you simply select all worksheets in your workbook using the tabs you can then set the zoom to 85% and it will apply to all worksheets

Sub SetZoom()
Dim ws As Worksheet
Application.ScreenUpdating = False 'Optional
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
ActiveWindow.Zoom = 85
Next
Application.ScreenUpdating = True
End Sub
This code is similar from the above, but it is not necessary to select all worksheets in your workbook before running the macro. Instead of using ws.Select and Next ws that not work correctly unless you select the worksheets, change to ws.Activate and Next to set the zoom for all the sheets. As optional, the ScreenUpdating can be disabled for a workbook with a lot of sheets.

Option Explicit
Sub FixSheets()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
ws.UsedRange.Select
ActiveWindow.Zoom = True 'Zoom sur la sélection
ActiveCell.Select
Next ws
End Sub

Related

Lock and unlock cells in Excel based on color in all worksheets

I am trying to create a vba macro to unlock all yellow cells (colour index 36) and lock all non-yellow cells. The below code works; however, I am struggling to get this in a For loop to have it run through each sheet in my workbook (and not just run on 'Sheet1').
Sub test()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
For Each cell In Sheet1.UsedRange
If cell.Interior.ColorIndex = 36 Then
cell.Locked = False
End If
Next
ws.Protect userinterfaceonly:=True
End Sub
Assuming you really do want this to run on every worksheet in your workbook, the following should work:
Sub test()
'declarations
Dim ws As Worksheet
'loop through each worksheet (ws) in active workbook
For Each ws In ActiveWorkbook.Worksheets
'remove protection
ws.Unprotect
'lock all cells
ws.UsedRange.Cells.Locked = True
'loop through used range
For Each cell In ws.UsedRange
'unlock cells based on color index
If cell.Interior.ColorIndex = 36 Then cell.MergeArea.Locked = False
Next
'reprotect sheet
ws.Protect userinterfaceonly:=True
Next
End Sub

Hide sheets after the particular sheet name occurrence

I have a multitude of sheets in my workbook. I want to hide some of them. The primary criterion can be hiding them beyond the sheet with some specified name.
In my case, this is the sheet named BoM
I want everything to be hidden behind this worksheet.
What I tried is:
Sub Sheethidden()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name > "BoM" Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
but it didn't work
Please, try the next way. You need to use the sheet Index as reference:
Sub Sheethidden()
Dim wsIndex As Long, i As Long
wsIndex = Worksheets("BoM").Index
For i = wsIndex + 1 To Worksheets.count
Worksheets(i).Visible = xlSheetHidden
Next i
End Sub
or you may keep your code and compare the sheets index:
Sub Sheethidden()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.index > Worksheets("BoM").Index Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Adapting your code, I would do as follow.
Public Sub HideSheetAfterName()
Const NAME_SHEET = "BoM"
Dim ws As Worksheet
Dim ws_pass As Boolean
ws_pass = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name = NAME_SHEET Then ws_pass = True
If ws_pass Then ws.Visible = xlSheetHidden
Next ws
End Sub
Since you know Excel worksheets in ThisWorkbook.Worksheets are ordered by order they appear in the Excel workbook, I suggest just to check when you find the desired NAME_SHEET.
Additional suggestion: always use ThisWorkbook before Worksheet object in order to force using the current workbook.

AutoFit all active rows / columns in a workbook

I want to create a macro autofit all rows and columns with values in them in across all worksheets in a workbook. This is what I have so far, I am stuck. Thank you in advance.
Sub AutoFitColumnsRows()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Next
starting_ws.Activate
End Sub
I am not sure why you need the other variables you have defined, but the basic loop looks like this:
For Each ws In ThisWorkbook.Worksheets
ws.Cells.EntireRow.AutoFit
ws.Cells.EntireColumn.AutoFit
Next

Un Hiding all sheets and go to specific sheet

Hi I have to macro to unhide all sheets but when I run it, it takes me to the bottom of a random sheet. I would like it to go, to a specific sheet and take me to the top of the sheet.
code
Sub Unhide_All_Tabs()
For Each ws In Sheets: ws.Visible = True: Next
I agree with #Mathieu Guindon and #braX.
This is what I would do too:
Option Explicit
Sub Unhide_All_Tabs()
Dim WB As Workbook
Dim WSht As Worksheet
Set WB = ActiveWorkbook
For Each WSht In WB.Worksheets
WSht.Visible = xlSheetVisible
Next
Set WSht = WB.Worksheets("Dashboard")
WSht.Activate
WSht.Cells(1, 1).Select
End Sub
Sub Unhide_All_Tabs()
For Each ws In Sheets: ws.Visible = True: Next
ThisWorkbook.Sheets("Dashboard").Visible = True
ThisWorkbook.Sheets("Dashboard").Select
ThisWorkbook.Sheets("Dashboard").Range("A1").Select

Uncheck all checkboxes across entire workbook via CommandButton

I would like to have a code that unchecks all checkboxes named "CheckBox1" for all sheets across the workbook. My current code unfortunately doesn't work, and I'm not sure why - it only works for the active sheet.
Private Sub CommandButton1_Click()
Dim Sheet As Worksheet
For Each Sheet In ThisWorkbook.Worksheets
Select Case CheckBox1.Value
Case True: CheckBox1.Value = False
End Select
Next
End Sub
This code iterates through all sheets (except sheets named Sheet100 and OtherSheet) and unchecks all your ActiveX checkboxes named CheckBox1
Sub uncheck_boxes()
Dim ws As Worksheet
Dim xbox As OLEObject
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet100" And ws.Name <> "OtherSheet" Then
For Each xbox In ws.OLEObjects
ws.OLEObjects("CheckBox1").Object.Value = False
Next
End If
Next
End Sub
To uncheck all ActiveX checkboxes in all sheets disregarding the names used
Sub uncheck_all_ActiveX_checkboxes()
Dim ws As Worksheet
Dim xbox As OLEObject
For Each ws In ThisWorkbook.Worksheets
For Each xbox In ws.OLEObjects
ws.OLEObjects(xbox.Name).Object.Value = False
Next
Next
End Sub
To uncheck all Form Control checkboxes on a spreadsheet use
Sub uncheck_forms_checkboxes()
Dim ws As Worksheet
Dim xshape As Shape
For Each ws In ThisWorkbook.Worksheets
For Each xshape In ws.Shapes
If xshape.Type = msoFormControl Then
xshape.ControlFormat.Value = False
End If
Next
Next
End Sub
[edited following comments]
Try this:
Sub test()
Dim ws As Excel.Worksheet
Dim s As Object
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Definitions" And ws.Name <> "fx" Then
Set s = Nothing
On Error Resume Next
Set s = ws.OLEObjects("CheckBox1")
On Error GoTo 0
If Not s Is Nothing Then
s.Object.Value = False
End If
End If
Next ws
End Sub
This is a global function (it doesn't belong to a particular sheet), but you can put it inside CommandButton1_Click() if you want.
You might not need the error blocking if your sheets (other than Definitions and fx) always contain CheckBox1. Alternatively remove that if statement.

Resources