Populate Combo Box from Userform Variable - vba

I have two simple pieces of code in a Userform and what I want to do, I believe is quite straight forward however I'm stuck!
Private Sub UserForm_Initialize()
Dim wkb As Workbook
With Me.CB_Excel_File
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In ***Me.CB_Excel_File.Value.Worksheets***
.AddItem wks.Name
Next wks
End With
End Sub
Its the piece with *** that I'm stuck on as I want to list all of the worksheets in the workbook that is selected by the user from the Userform_Initialize code.
Thanks in advance!

You need to pass the selected workbook name into the Workbooks collection as a variable, as so:
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In Workbooks(CB_Excel_File.Value).Worksheets
.AddItem wks.Name
Next wks
End With
End Sub

Related

Macro not locating ws in other wb when WorkbookOpen sub is ran

My codes seems to only locate the worksheets in the workbook that is currently opening. When I go back after the workbook is already open and hit F5 on the Workbook_Open sub it then locates all the worksheets in every open workbook like it should. Not sure if this is a limitation on Excel. Any pointers are helpful.
Essentially the goal of the code is to make clicking hyperlinks in other workbooks execute a macro housed in this workbook. If anyone knows of a better way to do that I am all ears.
In "ThisWorkbook"
Private Sub Workbook_Open()
Call CreateClassesNEWWB
End Sub
In "Module 2"
Public objCollection As Collection
Sub CreateClassesNEWWB()
'allows hyperlinks in other workbooks to be driven off sub in this workbook
Dim ws As Worksheet
Dim HyperlinksClass As cHyperlinks
'Create A New Instance Of The Collection
Set objCollection = New Collection
'Loop All Worksheets
For Each ws In Worksheets
'Create A New Class For This Worksheet
Set HyperlinksClass = New cHyperlinks
'Add The Worksheet To The Class
Set HyperlinksClass.obj = ws
'Add The Class To The Collection
objCollection.Add HyperlinksClass
Next ws
End Sub
In Class Module "cHyperlinks"
Private WithEvents Pws As Worksheet
Private Sub Pws_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.TextToDisplay
Case "Transpose": Call TransposeWire
Case "Research": Call Research
End Select
End Sub
Property Set obj(ByVal objWS As Worksheet)
Set Pws = objWS
End Property
Sub CreateClassesNEWWB()
'allows hyperlinks in other workbooks to be driven off sub in this workbook
Dim ws As Worksheet
Dim HyperlinksClass As cHyperlinks
'Create A New Instance Of The Collection
Set objCollection = New Collection
For Each wb In Workbooks '<loop workbooks
For Each ws In wb.Worksheets '<loop worksheets
objCollection.Add LinkHandler(ws)
Next ws
Next wb
End Sub
'I like "factory" functions partly to be sure
' I'm creating all objects as independent instances...
Function LinkHandler(ws As Worksheet) As cHyperlinks
Dim rv As New cHyperlinks
Set rv.obj = ws
Set LinkHandler = rv
End Function

VBA Name a Sheet with ComboBox

I have a commandButton which opens a UserForm to add a new Worksheet.
In this Userform is a ComboBox, where i can choose the machine type.
Now i want to create a new Worksheet with the Name of the machine type which was selected in the ComboBox.
This is my Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Sheets("Sheet1").Copy Before:=Sheets(10)
ws.Name = ComboBox1
[UserForm1].Hide
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Machine Type 1", "Machine Type 2")
End Sub
Is there a way to create a new sheet, which is a copy from Sheet1 and name it like the machine type from the ComboBox1?
Thanks for your help.
Try the code below, see notes in the code's comments:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Sheets("Sheet1").Copy Before:=Sheets(10)
Set ws = ActiveSheet ' <-- you need to set the worksheet object to the latest copied sheet
ws.Name = ComboBox1.Value '<-- now you can modify the name using the worksheet object
Me.Hide '<-- hide the user-form
End Sub
use
Private Sub CommandButton1_Click()
Sheets("Sheet1").Copy Before:=Sheets(Sheets.Count)
ActiveSheet.Name = ComboBox1.Value
Me.Hide
End Sub

combobox with different worksheets, that changes worksheets in code

i've a question:
I would like to have a Combobox where all the worksheets are displayed. If you select a worksheet, then the worksheets in the code needs to change to the worksheet that you selected. I've tried but can't program this.
easy example:
dim WRKsheet as worksheet
set worksheet = Combobox1.value
sheets(WRKsheet).activate
Do any of you guys know how i can succeed in this?
Grts
Use the following code in a User_Form Module
Private Sub ComboBox1_Change()
' select the worksheet selected in the ComboBox1
Worksheets(ComboBox1.Value).Activate
End Sub
Private Sub UserForm_Activate()
Dim Sht As Worksheet
' show all sheets names in thisworkbook in ComboBox1
For Each Sht In ThisWorkbook.Sheets
ComboBox1.AddItem Sht.Name
Next Sht
End Sub
Note: (you need to call the form from another module, with UserForm1.Show)

How to add a list of specific worksheets to the list in ComboBox VBA

I found a way to add all of the worksheets in the workbook into the list of the ComboBox on the UserForm, which is done with use of the following code
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
cmbSheet.AddItem ws.Name
Next ws
End Sub
My problem is that I only need to add some specific worksheets and not all of them. For example my user will select specific sheet and by clicking "Continue" button should end up on the selected worksheet to continue his/her task. My workbook holds several worksheets, some of which are used to output data (Reports) and some worksheets contain so called templates, which I want my user (only those) to be able to select from the ComboBox I have talked above.
Can you guys help me please?
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
if ws.Name like "*Template" then cmbSheet.AddItem ws.Name
Next ws
End Sub
perhaps you could use a naming convention for the worksheets?
if the reports you want the user to select all have the word temmplate in their name you could do something like this:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
if instr(lcase(ws.name),"template")<>0 then
cmbSheet.AddItem ws.Name
end if
Next ws
End Sub

Excel VBA: ListBox-UserForm Variable creation issue

Checked other similar questions on other websites, but could not find a solution.
I am trying to generate variables from the contents of a listbox-userform. The contents in the list are the names of workbooks. The first piece of code shows you how I generate the contents of the list for your reference. The second piece of code is the one with the issue.
Private Sub CommandButton1_Click()
Dim wb As Workbook
For Each wb In Workbooks
With ListBox1
.AddItem (wb.Name)
End With
Next wb
lbl_Exit:
End Sub
I am getting a Object Required error on the For Each line below. This piece of code resides in the Userform.
Private Sub CommandButton3_Click()
Dim MainBook As Workbook
Dim ListBox1 As ListBox
For Each Item In ListBox1
If Item.Name Like "Aggregate" Then
Item.Select
Set MainBook = Selection
End If
Next
End Sub
Note: I read that Item is a property of a ListBox which is where I got that from.
Is this what you are trying?
Private Sub CommandButton1_Click()
Dim wb As Workbook
For Each wb In Workbooks
With ListBox1
.AddItem (wb.Name)
End With
Next wb
End Sub
Private Sub CommandButton2_Click()
Dim MainBook As Workbook, i as Long
For i = 0 To (ListBox1.ListCount -1)
If ListBox1.List(i) Like "Aggregate" Then
Set MainBook = Workbooks(ListBox1.List(i))
MainBook.Activate
Exit For
End If
Next
End Sub