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
Related
Private Sub UserForm_Initialize()
'declare the variable
Dim Sh As Variant
Dim cc As Variant
cc = Sheet4.Range("C4").Value
'for each loop to add sheets based on permission rank
For Each Sh In ActiveWorkbook.Sheets
'add only permitted sheet(ranking 1 to 6) and exclude settings sheet
If Sh.Range("G1").Value >= cc And Sh.Name <> "settings" Then
'add sheets to the list box
Me.lstSheet.AddItem Sh.Name
End If
Next Sh
End Sub
Try adding Me.Repaint after Me.1stSheet.AddItem
This will refresh the Form.
Consider changing your ListBox variable name from 1stSheet to sheet1ListBox too, to make it more descriptive and easier to debug.
Trying to create a excel worksheet using data from a VBA form then adding it to end of workbook. Please help to activate the code
Private Sub Add_Tab_Click()
Dim txtNameSur As Worksheet
Set txtNameSur = Worksheets("Me.Textbox1")
ThisWorkbook.Sheets(1).Copy after:=Sheets(Sheets.Count)
Newname = Worksheets.Add.Name = Userform1.txtNameSur.Value
ActiveSheet.Name = Newname
End Sub
The goal in your question is a bit unclear, but I guess you meant to do something like below:
Private Sub Add_Tab_Click()
'copy first worksheet to the end of the workbook
ThisWorkbook.Worksheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
'set the copied worksheet to a variable `NewWorksheet`
Dim NewWorksheet As Worksheet
Set NewWorksheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'last one is the copied one
'give it a new name (use the text in Textbox1 as name)
NewWorksheet.Name = Me.Textbox1.Text
End Sub
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.
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.
I cannot seem to get a combobox to work. I'm trying to add a dropdown combobox that displays all worksheets in my workbook.
My first step was to create a combobox which adds the sheets but the box does not add any sheetnames
Here is my code:
Sub ComboBox1_Change()
Dim WS As Worksheet
For Each WS In Worksheets
ComboBox1.AddItem (WS.Name)
Next WS
End Sub
The problem is that you add items through the Event 'Change'. If there is no change in the combobox (because there aren't any items) this event never fires.
Instead you can add your code to the Initialize or Activate Event of your Form. For example:
Private Sub UserForm_Activate()
Dim WS As Worksheet
For Each WS In Worksheets
ComboBox1.AddItem (WS.Name)
Next WS
End Sub
This will proper result
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> "Main" Then
Me.ComboBoxpgname.AddItem ws.Name
End If
Next ws
End Sub