I have an issue with below code.
I want to delete all visible sheets but a certain sheet, when a user closes the workbook.
This is the code:
Private Sub workbook_BeforeClose(cancel As Boolean)
Dim ws As Worksheet
For Each ws In Workbook
If ws.Visible = xlSheetVisible Then
If ws.Name <> "Choose BU" Then ws.Delete
End If
Next ws
End Sub
It says "Object required", however I thought the worksheet per default is an object in VBA?
You could try:
Option Explicit
Sub test()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
With ws
If .Visible = True And .Name <> "Choose BU" Then
.Delete
End If
End With
Next ws
Application.DisplayAlerts = True
End Sub
Related
I'm trying to delete any sheet with a title that is not "PR11_P3".
In the remaining sheet there is a table "PR11_P3_Tabell" which is by now always filtered or rather sorted somehow and this is what I'm trying to restore with .ShowAllData.
Sub DeleteSheetRestoreSort()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "PR11_P3" Then
ws.Delete
Then
ActiveSheet.ShowAllData
End If
Next ws
End Sub
There is no guarantee that ActiveSheet is the sheet you want to clear. Use ws instead.
Sub DeleteSheetRestoreSort()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "PR11_P3" Then
ws.Delete
Else
If ws.filtermode then
ws.ShowAllData
End If
End If
Next ws
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I made the macro code so that it checks cell "w6" in all worksheets when the excel file opens. When "w6" is empty, it should hide graph "FG" and only show graph "F".
When "w6" is not empty, it should hide graph "F" and only show graph "FG". I set the names of each graph as "F" and "FG". But there is an error message in the
wsht.Shapes.Range(Array("FG")).Visible = msoFalse line in the HideFG macro that "the item with the specified name wasn't found." I am sure that the graph name is "FG", but why is this happening? Is there an excel genius who can solve this?
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG ws
Else
HideF ws
End If
End With
Next
End Sub
Sub HideF(wsht As Worksheet)
'
' HideF Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG(wsht As Worksheet)
'
' HideFG Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Instead of calling another macro, you could just simply try the following:
If what you refer to are actually Shapes:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Shapes.Range(Array("F", "FG")).Visible = False
If ws.[W6] = 0 Then
ws.Shapes("F").Visible = True
Else
ws.Shapes("FG").Visible = True
End If
Next ws
End Sub
Or when they actually are ChartObjects then:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.[W6] = 0 Then
ws.ChartObjects("F").Visible = True
ws.ChartObjects("FG").Visible = False
Else
ws.ChartObjects("F").Visible = False
ws.ChartObjects("FG").Visible = True
End If
Next ws
End Sub
As per this question of yours, I feel you might not want to loop all the sheets.
In that case, change:
For Each ws In ThisWorkbook.Sheets
Into:
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
Let me know if it works for you :)
I am trying to add a couple more worksheets after my master worksheet in a VBA code. I have a button to initiate the macro which I have the master module and the CreateSheet module tied to that button.
Sub CreateSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Tempo"
End Sub
This is what I have in the CreateSheet module, the code runs but no sheets are created.
Public polerow As XlRowCol
Public MRBook, RDBook As Workbook
Sheets(1).Name = "Make-Ready"
Set MRBook = Worksheets("Make-Ready").Parent
This is what I have in the master module that produces all my output. I really would prefer to keep my macro to separate modules like this.
Try doing this, you can always use the same procedure without the need to change it's code:
Sub NewSheet(SheetName As String)
Dim SheetExists As Boolean
With ThisWorkbook
On Error Resume Next
SheetExists = (.Worksheets(SheetName).Name <> "")
On Error GoTo 0
If SheetExists = False Then
.Sheets.Add After:=.Sheets(.Sheets.Count)
.Sheets(.Sheets.Count).Name = SheetName
Else
Application.DisplayAlerts = False
.Sheets(SheetName).Delete
Application.DisplayAlerts = True
.Sheets.Add After:=.Sheets(.Sheets.Count)
.Sheets(wb.Sheets.Count).Name = SheetName
End If
End With
End Sub
'to call the newsheet you can do this
Sub Test()
NewSheet "Make-Ready"
End Sub
I use few macros based on Worksheet events and module to make built in timer. If someone didn't make any edition in the file for XX minutes, it will be saved and closed. It works generally but the code breaks from time to time on part ws.Visible = xlVeryHidden
I reproduced it few times to locate source of my problem.
It breaks only if that workbook is not as active used. If i work with other workbook and the timer hits XX it breaks my code.
I need those loops to set my sheets to START Values.
It is important for my User Force Macro to be active. User see only at the beggining sheet"START". If they chose to activate all macros. Sheet START will be hidden and other will be shown.
Module macros:
Option Explicit
Dim CloseTime As Date
Sub TimeSetting()
CloseTime = Now + TimeValue("00:00:30")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
Dim ws As Worksheet
'Step 1: Unhide the Starting Sheet
Sheets("START").Visible = xlSheetVisible
For Each ws In ThisWorkbook.Worksheets
'Step 2: Check each worksheet name
If ws.Name <> "START" Then
ws.Visible = xlVeryHidden 'Step 3: Hide the sheet
End If
Next ws 'Step 4: Loop to next worksheet
ActiveWorkbook.Close Savechanges:=True
End Sub
Workbook macros:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call TimeStop
ActiveWorkbook.Save
End Sub
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
Sheets("START").Visible = xlVeryHidden
Call TimeSetting
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call TimeStop
Call TimeSetting
End Sub
Sub SavedAndClose()
Dim ws As Worksheet
Application.ScreenUpdating = False
'Step 1: Unhide the Starting Sheet
On Error GoTo Error
Sheets("START").Visible = xlSheetVisible
'Sheets("START").Select
For Each ws In ThisWorkbook.Worksheets
'Step 2: Check each worksheet name
If ws.Name <> "START" Then
'Step 3: Hide the sheet
ws.Visible = xlVeryHidden
End If
'Step 4: Loop to next worksheet
Next ws
Error:
Sheets("START").Visible = xlSheetVisible
Application.ScreenUpdating = True
ThisWorkbook.Close Savechanges:=True
End Sub
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.