Method 'Visible' of object '_Worksheet' failed - excel

Not sure why this is failing but for whatever reason, when the ActiveSheet is Console it fails. Going the other way, it works properly. Code is below.
Sub Switch_Books()
Dim ws As Worksheet
protect_book True
If ActiveSheet.Name = "Console" Then
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "CDA Console" Then
ws.Visible = xlSheetVisible
Else
ws.Visible = xlSheetHidden
End If
Next ws
Else
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Console" Then
ws.Visible = xlSheetVisible
Else
ws.Visible = xlSheetHidden
End If
Next ws
End If
protect_book False
End Sub

Try this:
Sub Switch_Books()
Dim ws As Worksheet, wsName
protect_book True 'This is a confusing call...
' you should switch the way the boolean works
wsName = IIf(ActiveSheet.Name = "Console", "CDA Console", "Console")
ThisWorkbook.Sheets(wsName).Visible = xlSheetVisible 'must be at least one sheet visible
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> wsName Then ws.Visible = xlSheetHidden
Next ws
protect_book False
End Sub

Related

.ShowAllData on remaining sheet after all others deleted

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

Delete all visible sheets but a specific sheet

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

how to hide and unhide worksheet using data entry from userform

I don't know why am getting out of range subscript error. When I click on combobox1 and select an item, MaternityForm combobox is populated with worksheets in my workbook. Then I want to hide other worksheets apart from the one selected in MaternityForm. The active sheet will then receive data from userform but I am getting subscript out of range error..
Private Sub Get_Data_Click()
Dim ws As Worksheet
Dim xWs As Worksheet
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Visible = True
Next
Set ws = Worksheets(MaternityForm.Value)
Sheets(MaternityForm.Value).Activate
On Error Resume Next
For Each ws In Application.ActiveWorkbook.Worksheets
if ws.Name <> MaternityForm.Value Then
ws.Visible = xlSheetHidden
End If
Next
With Sheets(MaternityForm.Value)
.Range("B3").Value = Me.NameBox.Text
.Range("f3").Value = Me.PaynoBox.Text
.Range("B6").Value = Me.DTPicker1.Value
.Range("B7").Value = Me.DTPicker2.Value
.Range("B17").Value = Me.FirstPayBox.Value
.Range("B18").Value = Me.SecondPayBox.Value
.Range("B25").Value = Me.MonthlyPayBox.Value
.Range("H7").Value = Me.DTPicker3.Value
End With
End Sub
You are confusing your variables ws and xWs.
ws is referring to a specific sheet while xWs is your variable worksheet.
Therefore, your second loop is invalid (This is like saying For Each Sheet1 in Worksheets).
You need to loop through your variable worksheets and compare them to your specific sheet
For Each xWs In Application.ActiveWorkbook.Worksheets
if xWs.Name <> ws.Name Then
xWs.Visible = xlSheetHidden
End If
Next
With that being said, there is no need to loop twice.
Note that ws.Name = MaterityForm.Value will return either TRUE or FALSE. The result of this determines ws.Visible = TRUE/FALSE
Private Sub Get_Data_Click()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Visible = ws.Name = MaternityForm.Value
Next ws
With Sheets(MaternityForm.Value)
.Range("B3").Value = Me.NameBox.Text
.Range("f3").Value = Me.PaynoBox.Text
.Range("B6").Value = Me.DTPicker1.Value
.Range("B7").Value = Me.DTPicker2.Value
.Range("B17").Value = Me.FirstPayBox.Value
.Range("B18").Value = Me.SecondPayBox.Value
.Range("B25").Value = Me.MonthlyPayBox.Value
.Range("H7").Value = Me.DTPicker3.Value
End With
End Sub

There is a runtime error 91 on for loop

There is runtime error 91 on for loop need help!!
Sub clearSheet(WSName As String)
Dim ws As Worksheet
Set ws = Nothing
With ActiveWorkbook
Dim blWSExists As Boolean
blWSExists = False
For i = 1 To .Sheets.Count
If .Sheets(i).Name = WSName Then
blWSExists = True
.Sheets(i).Activate
.Sheets(i).Visible = xlSheetVisible
End If
Next
If Not blWSExists Then
Set ws = .Sheets.Add
ws.Move after:=.Sheets(.Sheets.Count)
ws.Name = WSName
ws.Visible = xlSheetVisible
End If
.Sheets(WSName).AutoFilterMode = False
.Sheets(WSName).Cells.Clear
.Sheets(WSName).UsedRange.ClearOutline
.Sheets(WSName).Cells.ClearFormats
End With
End Sub
Try that:
Dim ws As Worksheet
Dim blWSExists As Boolean
blWSExists = False
For Each ws In Worksheets
If ws.Name = WSName Then
blWSExists = True
End If
Next
If Not blWSExists Then
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = WSName
End If
Set ws = ActiveWorkbook.Sheets(WSName)
ws.AutoFilterMode = False
ws.Cells.Clear
ws.UsedRange.ClearOutline
ws.Cells.ClearFormats
ws.Activate
You can use Cells.Delete() to clear everything on a Worksheet:
Sub clearSheet(WSName As String)
Dim s As Object
For Each s in ThisWorkbook.Sheets
If s.Name = WSName Then
s.Visible = xlSheetVisible
If TypeOf s Is worksheet Then s.Cells.Delete ' not all Sheets have cells, but all Worksheets do
Exit Sub ' to ignore the rest of the code if the Sheet exists
End If
Next
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = WSName
End Sub
or just delete the sheet and add new to clear absolutely everything associated with the sheet:
Sub clearSheet(WSName As String)
On Error Resume Next
Sheets(WSName).Delete
Sheets.Add(After:=Sheets(Sheets.Count)).Name = WSName
End Sub

Print all sheets in workbook except for 3 specific sheets

I want to print all the sheets in the workbook I am working in except for Three specific sheets called "Front Page", "Data" and "Logs". I've tried with an "and"- and an "or"-statement and wrapped parenthesis around and nothing worked.
Here's the code:
Sub printsheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Front Page" And _
ws.Name <> "Data" And _
ws.Name <> "Logs" Then
ws.PrintOut
End If
Next ws
End Sub
It appears that you have hidden worksheets in your workbook.
Before printing them, you need to unhide them, or check if the worksheet is hidden. Either manually, or (especially if your sheets were hidden programmatically with xlSheetVeryHidden):
ws.Visible = xlSheetVisible ' ADD THIS LINE TO UNHIDE A SHEET
ws.PrintOut
Or
If ws.Visible = xlSheetVisible Then
ws.PrintOut
End if
Try something like this if you do not want to print out hidden worksheets:
Sub printsheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = True Then
If ws.Name <> "Front Page" And _
ws.Name <> "Data" And _
ws.Name <> "Logs" Then
With ws.PageSetup
.PrintArea = "b2:g26" ' USE YOUR PRINTAREA
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ws.PrintOut
End If
End If
Next ws
End Sub
You can also use one list with the sheets that you don't want to print
Sub printsheets()
Dim dontPrint As Object
Dim ws As Worksheet
Set dontPrint = CreateObject("Scripting.Dictionary")
dontPrint.Add "Front Page", 1
dontPrint.Add "Data", 2
dontPrint.Add "Logs", 3
For Each ws In ActiveWorkbook.Worksheets
If dontPrint.Exists(ws.Name) Then
Else
ws.PrintOut
End If
Next ws
End Sub

Resources