hide sheet that not in array - excel

I made array with the name of sheets that I want to select.
all the other sheets I want to hide.
its need to by dynamic.
list say that my array is
Sheets(Array("Sheets1", "sheet2")).Select
and I have 4 sheets
how I can hide all the others sheet that not in the array ?

Hide Sheets
The Sheets collection includes worksheets, charts, and whatnot.
Here is a similar solution for worksheets.
Option Explicit
Sub HideSheets()
' Do NOT hide:
Dim Exceptions As Variant: Exceptions = Array("Sheet1", "Sheet2")
Dim sh As Object
For Each sh In ThisWorkbook.Sheets
If IsError(Application.Match(sh.Name, Exceptions, 0)) Then
On Error Resume Next ' when trying to hide last visible sheet
If Not sh.Visible = xlSheetHidden Then
sh.Visible = xlSheetHidden
End If
On Error GoTo 0
End If
Next sh
End Sub

Related

Loop through worksheets, exclude some and look-up cell value

I have an Excel workbook with several worksheets.
I would like the macro to look at the value of cell "A1" of each worksheet.
If the cell value is less than 8, A1 must be adjusted to 8.
If the cell value is greater than 8, nothing needs to be adjusted.
I have two macros:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.Name
Case Is = "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2
End Select
Next sh
End Sub
and
Sub X2()
'declare a variable
Dim ws As Worksheet
Set ws = ActiveSheet
'calculate if a cell is less than a specific value
If ws.Range("A1") < 8 Then
ws.Range("A1") = 8
Else
End If
End Sub
The problem is that only the active worksheet is done and the rest of the worksheets are not looked at. The macro also does not check whether the worksheet should not be included.
If you want using two subs, please try the next way. Your code only use the active sheet in the second sub:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.name
Case "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2(sh)
End Select
Next sh
End Sub
Sub X2(ws As Worksheet)
'calculate if a cell is less than a specific value
If ws.Range("A1").value < 8 Then ws.Range("A1") = 8
End Sub
But for such a simple processing, no need of the second since everything can be done in the first one:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.name
Case "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
If sh.Range("A1").value < 8 Then sh.Range("A1") = 8
End Select
Next sh
End Sub
The cleanest way to do this with the code in its current form would be to pass the sheet object to the other sub:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.Name
Case Is = "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2(sh)
End Select
Next sh
End Sub
and then
Sub X2(ByVal sh As Worksheet)
'calculate if a cell is less than a specific value
If sh.Range("A1") < 8 Then
sh.Range("A1") = 8
End If
End Sub
I've assumed you have a reason in the real-world use for having separate sub-routines, but once you understand this concept of passing the objects around then I would suggest just doing this in a single routine.

Delete multiple Excel Sheets in VBA

I am using an excel Workbook for programtical generation. Once the workbook is created few of the sheets are having required data and few are blank with default templates only.
I need to delete all sheets having default templates (means no data). I can check specific cell to identify this however need to know how to check for all sheets and then delete sheets one by one.
I am having this piece of code:
Sub TestCellA1()
'Test if the value is cell D22 is blank/empty
If IsEmpty(Range("D22").Value) = True Then
MsgBox "Cell A1 is empty"
End If
End Sub
Try this:
Sub DeleteEmptySheets()
Dim i As Long, ws As Worksheet
' we don't want alerts about confirmation of deleting of worksheet
Application.DisplayAlerts = False
For i = Worksheets.Count To 1 Step -1
Set ws = Worksheets(i)
' check if cell D22 is empty
If IsEmpty(ws.Range("D22")) Then
Sheets(i).Delete
End If
Next
' turn alerts back on
Application.DisplayAlerts = True
End Sub
An alternative implementation using For-Each:
Sub deleteSheets()
Dim wb As Workbook
Dim sht As Worksheet
Set wb = Workbooks("Name of your Workbook")
'Set wb = ThisWorkbook You can use this if the code is in the workbook you want to work with
Application.DisplayAlerts = False 'skip the warning message, the sheets will be deleted without confirmation by the user.
For Each sht In wb.Worksheets
If IsEmpty(sht.Range("D22")) And wb.Worksheets.Count > 1 then
sht.Delete
End If
Next sht
Application.DisplayAlerts = True
End Sub
This mainly serves as a demonstration pf how you can easily loop through worksheets.
As suggested in the comments below by #Darren Bartrup-Cook , the logic according to which the sheets are deleted can and should be modified to not only suit your purposes but to also include safeguards.
Making sure there's always at least one worksheet in the workbook is one of them. This can be ensured in a multitude of ways. I updated my answer to implement one these.

Clear tab color for specific selected sheets with vba macro

I would like to delete sheet tab color for different sheets. E.g. only for sheets if their names containing "Defects".
Sub Simples()
Dim ws As Worksheet
For Each ws In Worksheets
If InStr("Defects", ws.Name) > 0 Then
ws.Tab.ColorIndex = xlColorIndexNone
End If
Next ws
End Sub

List hidden worksheets

I have a materials register I am creating
Due to regulation when a material (each material has its own worksheet with a 3 digit random number added on the end to allow the same name multiple times) is deleted it cannot actually be deleted, so to work around this my workbook hides the sheet and using a deletion check on the summary page hides the appropriate row.
However what I am struggling with is a function to restore the sheet,
I have the code I need to do this however I cannot find any function to list hidden sheets.
This list can be put into the work book in a hidden column so I can reference it with my macro but as I said I cannot find anyway to list only sheets that are hidden.
Thanks for your help
You could add to your code that does the hiding to write the name of the sheet that it is hiding to your other hidden tab, and add the reverse to your code that unhides it.
Not sure if the below is applicable to your situation, but you could also put some code in worksheet events to capture when the sheet is being made invisible
Private Sub Worksheet_Deactivate()
If Me.Visible = xlSheetHidden Then MsgBox "I have been hidden"
End Sub
Does this help ..
' Function to be used in array formula on sheet to list hidden sheets
Public Function ListHiddenSheets()
Dim hiddenSheets As New dictionary
Dim sheet As Worksheet
For Each sheet In Worksheets
If sheet.Visible <> xlSheetVisible Then hiddenSheets.Add sheet.Name, Null
Next sheet
Dim vRes() As Variant
ReDim vRes(0 To hiddenSheets.Count, 0 To 0)
Dim idx As Integer
For idx = 0 To hiddenSheets.Count - 1
vRes(idx, 0) = hiddenSheets.keys(idx)
Next idx
ListHiddenSheets = vRes
End Function
?
Hidden sheets can be Hidden or VeryHidden, to capture these:
ub ListEm()
Dim ws As Worksheet
Dim StrHid As String
Dim strVHid As String
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Visible
Case xlSheetVisible
Case xlSheetHidden
StrHid = StrHid & ws.Name & vbNewLine
Case Else
strVHid = strVHid & ws.Name & vbNewLine
End Select
Next
If Len(StrHid) > 0 Then MsgBox StrHid, vbOKCancel, "Hidden Sheets"
If Len(strVHid) > 0 Then MsgBox strVHid, vbOKCancel, "Very Hidden Sheets"
End Sub

VBA to Hide specific sheets listed in one sheet named "Summery"

I tried based on the following code. It mentioned below. Actually I want to hide a particular list of sheets which were given in one sheet named "Summery" from range (7,8) to (40,8). The following is the code:
Private Sub ToggleButton2_Click()
Dim sheet As Worksheet
rc = 40
For r = 7 To rc
sheet = Sheet37.Cells(r, 8)
Sheet.Visible = xlSheetHidden
Next
End Sub
But it was not working. Please help.
Instead of
sheet = Sheet37.Cells(r, 8)
Use
Set sheet = Worksheets(Sheet37.Cells(r, 8))
You should also consider adding error handling in case the listed sheet name does not exist
Since you use a Toggle button, I assume you want to unhide it on the second click? So maybe try this:
Private Sub ToggleButton1_Click()
Dim sh As Worksheet, ws As Worksheet
Dim shList As Variant
Set ws = ThisWorkbook.Sheets("Summery")
shList = Application.Transpose(ws.Range("H7:H40"))
For Each sh In ThisWorkbook.Sheets
If Not IsError(Application.Match(sh.Name, shList, 0)) Then
sh.Visible = Not sh.Visible
End If
Next
End Sub

Resources