ComboBox load the Worksheet names into Dropdown List - excel

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

Related

How to Make Destination Workbook Dynamic When Moving Data From Another User Selected File Using A VBA Macro in Excel

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!

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

Set Visibility and Invisibility for Two Shapes in all of sheets in Workbook by VBA

I wanna set visibility and invisibility for two shapes at the same time by VBA.
I wrote this code:
Sub Set_Visible_Invisible()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.ActiveSheet.Shapes("btn_S2_Pasive").Visible = False
ws.ActiveSheet.Shapes("btn_S2_Active").Visible = True
Next ws
End Sub
but it's work for only active sheet, not all of the sheets that the workbook has.
Any idea is welcome.
Perhaps:
Sub Set_Visible_Invisible()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Shapes("btn_S2_Pasive").Visible = False
ws.Shapes("btn_S2_Active").Visible = True
Next ws
End Sub
The key issue is not to double specify the worksheet.

go to specific cell when a worksheet is selected

I want to go to specific cell when a worksheet is selected
Private Sub Worksheet_Cellselection()
ActiveSheet Goto:="D5"
End Sub
That one does not work
You need to use the Worksheet_Activate event of the relevant worksheet:
Private Sub Worksheet_Activate()
Range("D5").Select
End Sub
To use for several worksheets in a Workbook, move the code to Workbook_SheetActivate evenrt (inside the Workbook level):
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Select Case Sh.Name
Case "Sheet1", "Sheet2", "Sheet4" '<-- run it only for sheet's with these names
Range("D5").Select
End Select
End Sub
Application.Goto ActiveSheet.Range("D5")

Resources