Hide/unhide sheet with specific name using VB - excel

I would like to write function, that will hide all sheets in my workbook where sheet name begin with specific characters (sheet name will begin with characters: frm -> frmList, frmCheck, frmLoad, ...).
Then I would like to show/unhide specific sheet when I will call them. Let say that I am in Sheet1 and I would like to show (open) frmList (when go back to Sheet1, frmList should became invisible again).
Thanks.
What I do was to put code (for hidining)
Private Sub Worksheet_Deactivate()
Me.Visible = xlSheetVeryHidden
End Sub
in every sheet I would like to hide. Is there any option to use for all shets where name begin with specific characters (frm)?
For unhiding sheets I use:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim shtName As String
'shtName = Target.Name
shtName = Left(Target.SubAddress, InStr(1, Target.SubAddress, "!") - 1)
Sheets(shtName).Visible = xlSheetVisible
Sheets(shtName).Select
End Sub
EDIT with loop
Ok, I loop for sheet name and hide them.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.name Like "obr" & "*" Then
'''ws.Range("A1").Interior.ColorIndex = 37
' ws.Me.Visible = xlSheetVeryHidden
ws.Visible = xlSheetHidden
End If
Next ws
If I use as Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) I can not update my forms, because every time I change something inn those sheet it hide it(when press enter/return). How can I set, that if sheet is active, will not hide after pressing enter. It should hide only when go back to other sheet via hyperlink (click on text with hyperlink to "main" sheet) or just click on other sheet name.

Related

Combobox event not firing

I have a combobox (drop down list) that is populated with the names of all the sheets in the workbook. When I select one of them, it activates the selected sheet.
This was working until I copied it in another workbook and did some changes.
Here's the code I use to populate the combobox (which still works):
Sub fillAllCombos()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name <> PVTSHEET Then Call fillCombobox(ws.Name)
Next
End Sub
Sub fillCombobox(wsName As String)
Dim ws As Worksheet
Dim oCmbBox As Object
Set oCmbBox = ThisWorkbook.Sheets(wsName).Shapes("cmbSheet")
oCmbBox.ControlFormat.RemoveAllItems
For Each ws In ThisWorkbook.Sheets
oCmbBox.ControlFormat.AddItem ws.Name
Next
End Sub
Here's how I capture the event:
Sub CmbSheet_Change()
Dim oCmbBox As Object
Set oCmbBox = ActiveSheet.Shapes("cmbSheet")
With oCmbBox.ControlFormat
If .Value <> "" Then
ActiveWorkbook.Sheets(.Value).Activate
.ListIndex = 0
End If
End With
End Sub
The event-capture macros are in the sheet where the combobox is located.
In my search for answers I have also tried CmbSheet_Click()but same result.
I've named the combobox as in the image:
**Edit: Application.EnableEvents = True
It looks as if you have a Form Control Combobox. You can assign any parameter-less public sub to it, but you have to do this assignment manually. Just right-click on the control and select "Assign Macro...".
It's different for an ActiveX Control where the assignment is done via its name.

ComboBox selection of a workseet, hide and unhide worksheets

I created this VBA that linked to a comboBox which is on the main worksheet called "ActiveX". This comboBox populates the name of each existing worksheet automatically even if worksheet names are changed, worksheets are added or deleted. When a dropdown item of the combobox representing a worksheet is selected, the user is taken to the selected worksheet.
What I am working on, is trying to add to this:
Once on the selected worksheet, I want to add a button that takes me back to the main worksheet called “ActiveX”
Hide all worksheets apart from the selected worksheet and the worksheet called “activeX” (which is the main menu worksheet)
Since additional worksheets can occasionally be added or removed by the user, is it possible to automatically insert a button to the selected worksheet only. Instead of having to add a button per worksheet?
I am not proefficient at creating VBA code so I try to copy, amend and adapt from sites like this one.
`Private Sub cbSheet_Change()
If cbSheet.Value <> "Select Item" Then
Worksheets(cbSheet.Value).Select
End If
cbSheet.Value = "Select Item"
End Sub
Private Sub Worksheet_Activate()
Dim Sh As Worksheet
Me.cbSheet.Clear
For Each Sh In ThisWorkbook.Worksheets
Me.cbSheet.AddItem Sh.Name
Next Sh
End Sub
Hide and Unhide sheets
Sub Hide_SH()
Dim sh As Worksheet
For Each sh In Sheets
If sh.Name <> "ActiveX" Then sh.Visible = False
Next sh
End Sub
Sub UnHide_SH()
Dim sh As Worksheet
For Each sh In Sheets
sh.Visible = True
Next sh
End Sub
You can add a hyperlink to the selected sheet.
Private Sub cbSheet_Change()
Dim ws As Worksheet
If cbSheet.Value <> "Select Item" Then
Set ws = Sheets(Me.cbSheet.Value)
With ws
.Visible = True
.Select
.Hyperlinks.Add Anchor:=.Range("A1"), Address:="", SubAddress:= _
"ActiveX!A1", TextToDisplay:="ActiveX!A1"
End With
End If
cbSheet.Value = "Select Item"
End Sub

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

VBA Add second Sheet with same Name

I have a CommandButton which opens a UserForm and create a copied Sheet with the name of the ComboBox Value.
This is My Code:
Private Sub CommandButton1_Click()
[UserForm1].Show ' Open UserForm
End Sub
Private Sub CommandButton2_Click()
Dim ws As Worksheet
ActiveWorkbook.Sheets("Sheet1").Visible = True ' Unhide Sheet
Sheets("Sheet1").Copy _
Before:=ActiveWorkbook.Sheets("Sheet1") ' Copy Sheet
Set ws = ActiveSheet
ws.Name = ComboBox1.Value ' Name Sheet
[UserForm1].Hide ' Close UserForm
ActiveWorkbook.Sheets("Sheet1").Visible = False ' Hide Sheet again
End sub
Now my problem is, if there are two machines with name "Machine Type 1" Excel gets an Error. So what do i have to change in my code, that the second sheet would named e.g. "Machine Type 1 (2)?
Thanks for your help.
you could try this
Private Sub CommandButton1_Click()
If IsSheetThere(ComboBox1.Value) Then 'if some sheet with chosen name already there
Sheets(ComboBox1.Value).Copy Before:=Sheets(10) ' copy the existing sheet
With ActiveSheet 'reference just copied sheet
.UsedRange.Clear 'clear its content
Sheets("Sheet1").UsedRange.Copy ActiveSheet.Range("A1") ' copy Sheet1 content and paste into it
End With
Else 'otherwise
Sheets("Sheet1").Copy Before:=Sheets(Sheets.Count) ' make a copy of "Sheet1" sheet
ActiveSheet.Name = ComboBox1.Value 'and rename it as per chosen name
End If
Me.Hide
End Sub
Function IsSheetThere(shtName As String) As Boolean
On Error Resume Next
IsSheetThere = Not Sheets(shtName) Is Nothing
End Function
the code line:
Sheets(ComboBox1.Value).Copy Before:=Sheets(10) ' copy the existing sheet
is the one that leaves Excel the burden of somehow "counting" the number of already existing sheets with the chosen name, and name the new one appropriately
You can use the following sub which calls the below function, just apply the same logic using .Copy
Sub create_new_sheet_with_name(name As String, wb As Workbook, aftersheet As Variant)
Dim i As Integer
i = 2
If sheet_name_exists(name, wb) Then
Do While sheet_name_exists(name & " (" & i & ")", wb)
i = i + 1
Loop
wb.Sheets.Add(after:=aftersheet).name = name & " (" & i & ")"
Else
wb.Sheets.Add(after:=aftersheet).name = name
End If
End Sub
Function sheet_name_exists(name As String, wb As Workbook) As Boolean
For Each sheet In wb.Worksheets
If sheet.name = name Then
sheet_name_exists = True
Exit Function
End If
Next sheet
sheet_name_exists = False
End Function
here's an example of how to use the sub:
Sub test()
create_new_sheet_with_name "hi", ThisWorkbook, ThisWorkbook.Sheets(1)
'this adds a new sheet named "hi" to thisworkbook after thisworkbook.sheets(1)
End Sub
Technically this isn't an answer to this question... but it's better because it will help you solve this and many other coding tasks on your own.
There is a simple way to create VBA code for most basic tasks.
If there's something Excel can do that you want to be able to do programmatically, just Record a Macro of yourself performing the action(s), and then look at the code that Excel generated.
I have a terrible memory, I can't remember commands I used yesterday. So it's not only quicker and less frustrating for others for me to figure it out myself, but the more often I do that, the quicker I'll learn (without asking others to do the thinking for me on a basic question).
I fact, I'm guess that the majority of veteran VBA coders learned at least partly by analyzing recorded macros. I know I did.

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