Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Update Second UserForm Combobox Selection worksheet Info

0

This is a follow up from https://www.teachexcel.com/talk/7050/populate-combox-then-listbox-by-selection a question I could not update the workbook attachment. 

I have redesigned the 2nd userform which includes red boxes on what I want to occur.  When the "Customer_Info" is activated from the Main_Database you have to either select a current customer information or enter new customer information then click "Select Product Order" which will bring up the 2nd userform which I need help.  This works perfectly

Private Sub cmdOpGnPrd_Click()

'Click to open with the "Select Product Order" button

'Set screens to Full Screen

Dim xlws As XlWindowState
xlws = Application.WindowState
Application.WindowState = xlMaximized

With frmGunParts
    .Top = Application.Top
    .Left = Application.Left
    .Width = Application.Width
    .Height = Application.Height
End With

'Add Combobox value from Customer_info userfrom to label box on frmGunParts userform

MyVal = frmForm.cboCustNo.Value
frmGunParts.txtCustupdate.Value = MyVal

'Show frmGunParts userform before closing userFrom1 (Customer_Info frmForm)

frmGunParts.Show

Unload Me ' UserForm1 "frmForm"

Application.WindowState = xlws
End Sub

Which shows the 2nd userform.  I have added red comment boxes which shows what I would like for it to do or looking for suggestions if needed.  Each textbox, combobox and command button has a "red box comment" explaining what I would like to do. I have attached the workbook.

Answer
Discuss

Discussion

Divinedar - just noticed there isn't a file with this new question yet. You should be able to add it now. 
John_Ru (rep: 6142) Jan 27, '23 at 4:08 pm
Think you revised the question just but still no file. 
John_Ru (rep: 6142) Jan 27, '23 at 4:18 pm
I added the file when I wrote the question and I just selected "Edit" above and added the file and it's not taking it. I hit "Submit" so do I have to wait for another approval before you might be able to see it?

When I click "Discuss" it doesn't give me an option to add a file. I don't know.

I tried to resubmit so I'll see if they approve it.
Divinedar (rep: 20) Jan 27, '23 at 4:22 pm
You can only add files to questions or answers and they can only be Excel files. I don't ask questions so not sure but I think once Don has approved questions, you should be able add Excel files. Can't see one yet.
John_Ru (rep: 6142) Jan 27, '23 at 4:46 pm
Okay, I decided to give you a fuller answer (discussed in another thread). Hope you can Select the revised Answer.
John_Ru (rep: 6142) Jan 30, '23 at 8:47 am
Good morning. So Question:  If I want to add a file should I selecct "Answer" and not "Discuss"?  Because I would like to send you the file but I thought if you select "Answer" that closes the question.  Can you select "Answer" multiple times?
Divinedar (rep: 20) Jan 30, '23 at 12:23 pm
Good evening (here)! If you want to add a file, you have to do that when you edit your question.

Can't you work with the file I sent today? To be honest Ive done quite a but on this and you need to do some if the coding to link UserForms. My time is limited. 

You can only select an Answer once but you can Deselect it (if something doesn't work say) and select later - but that doesn't help me at all!
John_Ru (rep: 6142) Jan 30, '23 at 12:46 pm
Add to Discussion

Answers

0
Selected Answer

Divinedar

(Revision- this replaces the answer posted earlieir)

You didn't add a file to your question so instead (like before) I've given you an example which should help.

In the revised file attached, I'm proposing that there's a common start for parts list sheets. This is declared as a Constant at the top of of the UserForm1 code as follows:

Const Root As String = "PT-"

It could be anything but I've made it short so it doesn't restrict the sheet names too much (hopefully). I then copied your parts sheet and renamed it with that common start (and added a copy -with different checked boxes- as  "PT-Another model").

The first sheet now has a new button saying "Demo ListBox selections". Click that and new form UserForm1 is shown then initialised, populating the ComboBox (top right) with the model names (=sheet names less the Root) e.g. "Another model", as follows,with comments to help:

Private Sub UserForm_Initialize()

Dim ws As Worksheet, Ct As Shape

' loop through sheets and set data sources
For Each ws In ThisWorkbook.Worksheets
    ' check sheet name starts with root
    If InStr(ws.Name, Root) = 1 Then
        ' add name (less Root) to ComboBox list
        Me.ComboBox1.AddItem Split(ws.Name, Root)(1)
    End If

Next ws

End Sub

When the dropdown is chnaged (a model picked), this code populates the ListBox:

Private Sub ComboBox1_Change()

Dim Index As Long, Col As Long, Val As Variant

' do nothing if no model was picked
If Me.ComboBox1.Value = "" Then Exit Sub

' clear previous list
Me.ListBox2.Clear

'do nothing if an invalid name was typed in
On Error Resume Next
If ThisWorkbook.Worksheets(Root & Me.ComboBox1.Value).Name <> Root & Me.ComboBox1.Value Then Exit Sub

' with chosen source
With ThisWorkbook.Worksheets(Root & Me.ComboBox1.Value)

    ' populate  second list box with checked items only
    For Each Ct In .Shapes
        If InStr(Ct.Name, "Check Box") = 1 And Ct.OLEFormat.Object.Value = 1 Then
            ' populate columns of ListBox2
            Me.ListBox2.AddItem .Range(Ct.TopLeftCell.Address).Offset(0, 1).Text
            Index = 1
            ' loop through columns of checked item
            For Col = 2 To 4
                Val = .Range(Ct.TopLeftCell.Address).Offset(0, Col).Value
                ' format the currency column
                If Col = 4 Then Val = Format(Val, "$#,##0.00")
                Me.ListBox2.List(Me.ListBox2.ListCount - 1, Index) = Val
                ' increase the column index
                Index = Index + 1
            Next Col
        End If
    Next Ct
    ' set up LB2 columns
    Me.ListBox2.ColumnCount = 4
    Me.ListBox2.ColumnWidths = "40;200;40;40"

End With

End Sub

Change the sheet and the List will change.

Note that the List box has the property Multiselect set to 2 - fmMultiSelectExtended (which means you can control click several distinct items and/or click one and shift click another to pick those and the ones between.

When a sheet is chosen from the CombBox dropdown, the checked items are displayed as follows (I did the first 4 columns only):

When the green Submit button is clicked (after some selections), this sub delivers the selected items via this sub:

Private Sub Cb_Submit_Click()

Dim Picked As String

' loop through Listbox2

With Me.ListBox2
    For x = 0 To .ListCount - 1
        ' get Item# and Parts name (e.g.) from List
        If .Selected(x) = True Then
           Picked = Picked & .List(x, 0) & " " & .List(x, 1) & vbCrLf
        End If
    Next x
End With

' tell user
MsgBox "You picked:" & vbCrLf & vbCrLf & Picked & vbCrLf & "from " & Root & ComboBox1.Text
Unload Me


End Sub

This code whould work with many parts list sheets (provided they have whatever Root you choose).

Hope this gives you a good idea of what to do. You might just edit and rename my UserForm1.

Discuss

Discussion

Ok thank you
Divinedar (rep: 20) Jan 30, '23 at 1:14 pm
Thanks for selecting my Answer, Divinedar. Come back if you get stuck linking the userforms or whatever. 
John_Ru (rep: 6142) Jan 30, '23 at 1:35 pm
If you do ask another question, add your Excel file using the "Add files to the Post" button then use one or more of the "Choose file" buttons below that
John_Ru (rep: 6142) Jan 30, '23 at 1:52 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login