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.
Related
I'm attempting to have a user select a file, then through a UserForm select the worksheet they want to copy from.
The issue is on this line: ReportWbk.Sheets(ws).Cells.Copy Destination:=TargetWbk.Sheets("Test Import").Cells(1, 1)
I have confirmed that the file, worksheet selection, and copy function all work as expected. I get a run-time error '9': Subscript out of range. which to me means that the code is looking for the destination sheet in the workbook that the user selected (the 'target' file). Since it doesn't exist there, it errors out.
I'm wondering how to fix this dynamically so that the code will work without naming the workbook specifically. I might run this with other workbooks open and in different workbooks.
I verified that workbooks("WorkbookName").Sheets("Test Import").Cells(1, 1) works as I want but I'm looking to make the "WorkbookName" part dynamic / automatically select the workbook that the macro was run from.
The main code is directly below and the userform code is below that in case that's part of the issue:
Private Sub GetRange()
Dim ReportWbk As Workbook 'workbook with report data
Dim Report As Integer 'name of file with report data
Dim FD As FileDialog
Dim TargetWbk As Workbook 'this workbook
Dim ws As Variant
Set TargetWbk = ThisWorkbook
Set FD = Application.FileDialog(msoFileDialogFilePicker)
Report = FD.Show
'cancel pressed
If Report <> -1 Then Exit Sub
'open selected workbook
Set ReportWbk = Workbooks.Open(FD.SelectedItems(1))
ws = SelectSheet.Selection(ReportWbk)
'cancel pressed
If ws = vbCancel Then GoTo exitsub
ReportWbk.Sheets(ws).Cells.Copy Destination:=TargetWbk.Sheets("Test Import").Cells(1, 1)
exitsub:
ReportWbk.Close False
'changes the color of the destination worksheet tab
Worksheets("Test Import").Tab.Color = RGB(25, 25, 25)
End Sub
UserForm Code:
Private Sub cmdOK_Click()
Me.Tag = Me.ListBox1.Value
Me.Hide
End Sub
Private Sub cmdCancel_Click()
Me.Tag = vbCancel
Me.Hide
End Sub
Function Selection(ByVal wb As Object) As Variant
Dim ws As Worksheet
For Each ws In wb.Worksheets
'add all sheets to listbox
Me.ListBox1.AddItem ws.Name
Next ws
'default - select first sheet in list
With Me.ListBox1
.ListIndex = 0
.MultiSelect = fmMultiSelectSingle
End With
Me.Show
'return result
Selection = Me.Tag
Unload Me
End Function
Private Sub UserForm_Initialize()
Me.Caption = "Select Sheet"
Me.CmdOK.Caption = "OK"
Me.cmdCancel.Caption = "Cancel"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'disable X closing form
Cancel = CBool(CloseMode = 0)
If Cancel = -1 Then Me.Tag = vbCancel: Me.Hide
End Sub
I know this is a simple thing but I've been away from VBA for a few years and rusty as heck. Many thanks in advance!
I'm very new to VBA, and am trying to create a way to navigate sheets in a workbook via a dropdown list on a main 'index' page. As there are multiple sheets in the workbook, and multiple people using, I would like the dropdown list to autofill, to save from needing to search through the list. I would then like the selected sheet to open (possibly with a macro button, or even by hitting enter if that is an option).
So far, I have the following macro that populates the dropdown list (taken from another site):
Private Sub ComboBox1_DropButtonClick()
Dim xSheet As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
If ComboBox1.ListCount <> ThisWorkbook.Sheets.Count Then
ComboBox1.Clear
For Each xSheet In ThisWorkbook.Sheets
ComboBox1.AddItem xSheet.Name
Next xSheet
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This is perfect for the content of my dropdown list, and for its autofill feature; but I'm struggling to include that final action of actually going to the selected sheet. For info, I've used a ComboBox (ActivX Control). I was thinking it could be a case of creating a macro button and referencing the text in the dropdown selection?
Any help or suggestions would be much appreciated.
You can use the value in the combobox to switch tabs like this. Once I hit the button, then Sheet1 becomes active instead of Sheet2
Here's the code for you to copy/paste & test for yourself:
Private Sub CommandButton1_Click()
Dim mySheet As Worksheet
For Each mySheet In ActiveWorkbook.Worksheets
If mySheet.Name = ComboBox1 Then
mySheet.Select
Exit For
End If
Next mySheet
End Sub
Essentially, this will cycle through all your worksheets to find whatever is in the combobox. If there's a match, then it is selected.
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 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
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