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.
Related
I want to copy a hidden worksheet using VBA. However, When it run the vba code, all the copied worksheets are hidden as well, may I know is there any method to copy the worksheet and the new created worksheet is not hidden? My VBA code is as follows:
Sub CopySheet()
Sheet6.Copy After:=Sheets(Sheets.Count)
End Sub
There are two stages of Hidden, xlSheetHidden and xlSheetVeryHidden. On my Excel 365 your code worked for normal Hidden sheets and crashed for VeryHidden sheets. In neither case was the copy hidden. However, the following code will unhide the sheet, create a visible copy and hide the original again to the same level as it was before, all of that invisible to the user. This code can therefore be used for copying any sheet, regardless of the setting of its Visible property. It should work also on older versions of Excel.
Sub CopySheet()
Dim Visible As XlSheetVisibility
Application.ScreenUpdating = False ' hide the action from view
With Sheet6
Visible = .Visible ' record the sheet's visibility setting
.Visible = xlSheetVisible ' make the sheet visible
.Copy After:=Sheets(Sheets.Count) ' create a copy (the copy will be ActiveSheet)
.Visible = Visible ' reset the sheet's Visible property to what it was before
End With
Application.ScreenUpdating = True
End Sub
Please, try:
Sheet6.Copy After:=Sheets(Sheets.count)
Sheets(Sheets.count).Visible = xlSheetVisible
Sub CopySheet6()
Sheet6.Copy After:=Sheets(Sheets.Count)
Worksheets("Sheet6 (" & Sheets.Count - 1 & ")").Visible = True
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.
My workbook has three sheets(named Sheet1 ~ Sheet3) with TEXTBOXES.
This has a module.
Public dontDoThat As Boolean ' a public variable, visible throughout all your project you'll use to give way to synchronizing activity
Option Explicit
Sub Synchronize(txt As String, shtName As String)
dontDoThat = True ' set your public variable to True and prevent subsequent TextBox1_Change() events to run it again
Dim sht As Variant
For Each sht In Array("Sheet1", "Sheet2", "Sheet3")
If sht <> shtName Then Worksheets(sht).TextBox1.Text = txt
Next
dontDoThat = False ' set your public variable to False and allow subsequent TextBox1_Change() events to run it
End Sub
These code can synchronize the TEXTBOX on all sheets.
But It's only for Text.
If I type some text in TEXTBOX1 of Sheet1, the same text will be display in TEXTBOX1 of all other sheets.
but The search function does not work on other sheets.
After I typed some text on TEXTBOX1 of Sheet1 and When I press the enter key, The search function works only in Sheet1.
I want to trigger the enter keypress on Textboxes of all Sheets.
And these sheets also have TEXTBOX2.
Thus I want to know how to apply syncronization to TEXTBOX1 and TEXTBOX2 as well.
I need someone help.
Neo, I've done something like this before - where you create an autofilter either with Change event, or a specified KeyDown event (usually the Enter key - vbKeyReturn). Since you're using a KeyDown event, it wouldn't be overly exhaustive to use collections. You can, say, roll all of your TextBoxes in a collection either by name similarities, or by TypeName.
Specifically, for why I'm thinking you're having issues, perhaps loop through the Worksheets collection with an actual Worksheet object, and not a variant. And, since they're Worksheets objects, you can use OLEObjects.
Dim sht As WorkSheet
Dim x as Integer
x = 1
For Each sht In ThisWorkbook.WorkSheets
Do Until sht.OLEObjects("TextBox & x") is Nothing
If sht.Name <> shtName Then sht.OLEObjects("TextBox & x").Object.Text = txt
x = x + 1
Loop
Next
And even within that loop, you can do another loop to iterate over your multiple text boxes. Let us know how you want to proceed. Hope this helps...
I am working on a userform where a combobox gives user the names of every excel workbook that are already opened at that moment.
I want to put the names of all worksheets taking place in the workbook that is selected in the combobox on a listbox and I want it to be dynamic -that is, as the user selects another workbook from the combobox, the names (of the worksheets) appearing in the listbox should be automatically changing.
However, I can't figure out how I can access to the inventory of the combobox and make the desired additions. Any help/comment is appreciated. Thank you.
You can use the following functions
This one is for the UserForm, when it initializes it will fill in the combobox with all open sheets
Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To Workbooks.Count
ComboBox1.AddItem (Workbooks(i).Name)
Next
End Sub
This is about the Combobox itself, whenever you select some workbook from the combobox this function will be called and it is filling the list box with the sheets of that workbook.
Private Sub ComboBox1_Change()
Dim selected_wb As Workbook
Set selected_wb = Workbooks(ComboBox1.Text)
ListBox1.Clear
For Each ws In selected_wb.Worksheets
ListBox1.AddItem ws.Name
Next ws
End Sub
This function is called when you click the sheet name from the listbox, it will select the sheet and will close the user form
Private Sub ListBox1_Click()
Dim selected_wb As Workbook
Dim selected_ws As Worksheet
Set selected_wb = Workbooks(ComboBox1.Text)
Set selected_ws = selected_wb.Sheets(ListBox1.Text)
selected_ws.Activate
UserForm1.Hide
End Sub
I have a userform that fills out a row of information into an excel sheet. The excel sheet has two spreadsheets, one for data entry, and one for the 3 droplists that are in the userform. I want to delete this second sheet and make it into its own workbook. My question here is how can I write the VBA code to select the data from the droplist workbook (called "Client and Project Droplists.xlsx") to populate the droplists in the userform in the first workbook (called "Expense Reports Test.xlsm")? My current code is attached below.
Private Sub cboClient_Change()
Me.cboProject = ""
Select Case Me.cboClient
Case "Wells Fargo"
Me.cboProject.RowSource = "WellsFargoProjects"
Case "BLUSA"
Me.cboProject.RowSource = "BLUSAProjects"
Case "JP Morgan"
Me.cboProject.RowSource = "JPMProjects"
End Select
End Sub
I will be at work for the next few hours so any additional information can be requested in the questions/comments section. Would really appreciate help on this task.
My co-worker and I share a lot of data and work in excel quite a bit, so we have created quite a few shared tables on network drives for use in our utilities.
One method we have employed is opening a global list, copying it locally, and using it to populate a dropdown:
Sub GetStatusCodeList()
Dim ThisWb
Set ThisWb = ThisWorkbook
If Dir("\\SERVERNAME\GlobalUtilities\GlobalTables.xlsx") = "" Then Exit Sub
Application.ScreenUpdating = False
Workbooks.Open "\\SERVERNAME\GlobalUtilities\GlobalTables.xlsx", ReadOnly:=True
ActiveWorkbook.Sheets("GlobalTables").UsedRange.Copy ThisWb.Sheets("DropDown").Range("A1")
ActiveWorkbook.Close
Application.ScreenUpdating = True
End Sub
Another method simply reads the cells from a global list and writes them directly into the conditional formatting list. This particular code creates an array of available sheets and uses it to populate a dropdown:
Sub CreateSheetDropdown()
Dim sheetCounter, i
Dim theSheets() As String
ReDim theSheets(ActiveWorkbook.Sheets.Count + 1) As String
For i = 1 To ActiveWorkbook.Sheets.Count
theSheets(i) = ActiveWorkbook.Sheets(i).Name
Next i
With ThisWb.Sheets(Mtab).Range("SourceTabName")
.Value = theSheets(1)
.Validation.Delete
'.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
' Operator:=xlBetween, Formula1:=Join(theSheets, ",")
.Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=Join(theSheets, ",")
.Validation.ShowError = False
.Interior.color = RGB(250, 200, 200)
End With
End Sub
Finally, this code creates a dropdown in a userform from a global list that we keep on our shared drive:
Private Sub UpdateDropdowns()
Dim thisWorkbook
Set thisWorkbook = ActiveWorkbook
If Dir(TABLEPATH) = "" Then
MsgBox ("GlobalTables File Not Found - Critical Error")
Me.Hide
Exit Sub
End If
Workbooks.Open Filename:=TABLEPATH, ReadOnly:=True
'---------------------------------------------
'Method would load from GlobalTables.xlsx
'---------------------------------------------
'Load Utility Names
For Each c In ActiveWorkbook.Sheets(UTIL_SHEET).Range("A2:A" & ActiveWorkbook.Sheets(UTIL_SHEET).Cells(ActiveWorkbook.Sheets(UTIL_SHEET).Rows.Count, "A").End(xlUp).row).Cells
AddUtilToAll (c.Value)
Next c
End Sub
Private Sub AddUtilToAll(ByVal s)
For Each c In Me.Controls
If InStr(c.Name, "UtilityCombo") Then c.AddItem (s)
Next c
End Sub
Probably the easiest method to employ is the first one - just open the workbook stored on a shared drive and copy each dropdown list locally. You can run this in the Worksheet initialize function so that the dropdowns are updated each time you open the file.
Hope this helps, let me know if you want more information.
Edit:
It's probably easier to read here.
Just link your dropdown to a named range:
'Delete the old named range
ThisWorkbook.Names("TestDropdown").Delete
'Define the new named range
ThisWorkbook.Names.Add Name:="TestDropdown", RefersTo:=Range("A1:A25")