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

Populate Combox then Listbox by selection

0

I need to populate the Combobox on the "frmGunParts" from the worksheet "Colt_Gov_Model_MK_IV_80" worksheet columns "A3:C".  Column A is "checkboxes" so don't know if this can be done but check the box in column "A" and it's added to the listbox "lstGunOrder".  Once added to the listbox, that info is transfered to the "Customer_Order_History" worksheet to appropriate cells which is to include the Customer Number in label box "txtCustupdate". 

This is what I have so far that works perfectly that once you click "Select Product Order" on the Customer Info "frmForm". When testing be sure to select a customer or enter new customer info then select the "Select Product Order" button.

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

I have attached the workbook.  Thanks for any help I can get to begin this process.

Trying to attach an updated workbook.  Please update for attached.

Answer
Discuss

Discussion

Hi again Divinedar

I'm a bit confused on what you're trying to do here. Is the idea that the ListBox is populated from just those items checked on the parts sheet? Or that all the parts are displayed in the ListBox and chosen there?

I assume you intend to have several products so could you live with naming the parts sheets beginninig with a common start, say "Parts-"?
John_Ru (rep: 6142) Jan 26, '23 at 7:15 am
Yes. I want to be able to check the checkboxes from the combobox and whatever is checked is added to the listbox.

Or wondering if I should just make it a form to select parts from the combobox and fill in the other information to be transferred to the "Customer_Order_History"?  Maybe a listbox is not what I need.  I'm going to redesign the form and resend the file.  I need that when you pick a part from the parts list worksheet you fill in the information to go to the "Customer_Order_History".  Thank you gave me another idea.
Divinedar (rep: 20) Jan 26, '23 at 11:07 am
Okay, I won't have time today to complete but already did part of the code to populate the Listbox with only checked items.

Given your later comments, I think I need to see the revised file and question narrative anyway.
John_Ru (rep: 6142) Jan 26, '23 at 11:32 am
Ok perfect. No problem whenever you get a chance once I post.
Divinedar (rep: 20) Jan 26, '23 at 2:09 pm
I want to upload another file but it doesn't give me the option to do so.  How do I upload another file or are you only allowed one file upload?
Divinedar (rep: 20) Jan 26, '23 at 5:11 pm
So what I did was deleted the last file I uploaded and edited and submitted the updated file attached.  Let me know if you can see it.  I couldn't find another way to update the file. 

So what I did was update the userform and added "red label boxes" to explain what I needed each textbox, combobox, listbox and command button to do.  I didn't update any code.  The Customer Info userform works perfectly need help with the "Select Product Order" userform. Thank you.  Let me know if I should send anything else.
Divinedar (rep: 20) Jan 26, '23 at 5:46 pm
I can't see the editted file- there's no file attached to your question at present. Note that you can attach up to three files but only to a question and only Excel files. If you need to show a screenshot, you could embed it in an Excel file but bear in mind:

1) the question should be sufficiently complete for respondents to see if they can answer your question without looking at the file if possible
2) the Forum is about answering questions. We don't expect users to say "I'd like Excel to do a, b, c etc." and expect us to deliver a mini-project (but we are here to help when you get stuck in creating your own).

I'll check back later but have limited time again today.
John_Ru (rep: 6142) Jan 27, '23 at 3:18 am
Ok I'm going to close this and add another question.  I can't find where to update the workbook attached or pick an answer to my question. 
Divinedar (rep: 20) Jan 27, '23 at 12:28 pm
Oops. Added an Answer then spotted an error. Will fix over weekend
John_Ru (rep: 6142) Jan 27, '23 at 1:26 pm
Divinedar.

I have now corrected/revised my Answer/file so it's a "standalone" one (so as not to confuse other users). It has just one ListBox displaying all items. I'll add the "checked items" Listbox in your follow-up question.
John_Ru (rep: 6142) Jan 28, '23 at 5:33 am
Add to Discussion

Answers

0

Divinedar

You didn't add a revised file so instead I've given you an example which should help.

In the revised file attached, the first sheet has a new button saying "Demo ListBox selections". Click that and new form UserForm1 is shown then initialised as follows (with comments to help):

Private Sub UserForm_Initialize()

Dim Ct As Shape

' populate with all items
With Worksheets("Colt_Gov_Model_MK_IV_80")
   
   Set PrtsRng = .Range("B3:E" & .Cells(.Rows.Count, 2).End(xlUp).Row)
    Me.ListBox1.ColumnCount = 4
    Me.ListBox1.RowSource = .Name & "!" & PrtsRng.Address

End With

End Sub

REVISION 29 Jan 2023: only one list box is used in UserForm1

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) .

The variable PrtsRng is declared at a module level so it can be used when the Submit button is clicked: 

Dim PrtsRng As Range

Private Sub Cb_Submit_Click()

Dim Picked As String

' loop through Listbox1
For x = 0 To ListBox1.ListCount - 1
    ' get Item name (e.g.) from array
    If ListBox1.Selected(x) = True Then
       Picked = Picked & PrtsRng.Cells(x + 1, 2).Value & vbCrLf
    End If
Next x

MsgBox "You picked:" & vbCrLf & vbCrLf & Picked
Unload Me


End Sub

Note that items in list boxes have the first item as index zero (0), the second as one (1), and so on. In the range, I just add a 1 to the counter x to get to the right item.

Hope this gives you a good idea of how to do what you want to do.

Discuss

Discussion

Argh! Something went wrong on the checked listbox (and I don't have time to fix it today).
John_Ru (rep: 6142) Jan 27, '23 at 1:11 pm
Ok thank you.  I'm in no big rush so when you have the time is great help. I submitted another question with the updated file.  That seem to be the only way I could do it. Take your time no rush.
Divinedar (rep: 20) Jan 27, '23 at 2:00 pm
You should have been able to edit your original question and add a file (or remove a file and replace it) using the Remove/  Add Files... buttons
John_Ru (rep: 6142) Jan 27, '23 at 2:22 pm
I tried that by removing the last file and adding the new one but it didn't work or have not be approved yet one. I edited the question and added the new file and even put a comment at the end that I was updating the attach file but I haven't seen it yet.
Divinedar (rep: 20) Jan 27, '23 at 3:07 pm
There will be a delay since each new question gets screened by Don, owing to a massive spate (and still present) of spam questions. I can see your question now and hope to reply tomorrow or Sunday. 
John_Ru (rep: 6142) Jan 27, '23 at 4:06 pm
Ok. I think it maybe the name of the file. I tried renaming it to "Restoring Database Help File.xlsm". Tomorrow or Sunday is great. I'm working on it with the listbox code you sent me so I got something to work with. Thank you.
Divinedar (rep: 20) Jan 27, '23 at 4:29 pm
Thank you.

Is it possible for the first listbox to populate with worksheet names? The worksheet names will be similar but different for the ones to populate the first listbox.  When populated the second listbox is populated with that worksheet list to select from.

I'm looking at this code

Dim Ct As Shape

' populate with all items
With Worksheets("Colt_Gov_Model_MK_IV_80")
   
   Set PrtsRng = .Range("B3:E" & .Cells(.Rows.Count, 2).End(xlUp).Row)
    Me.ListBox1.ColumnCount = 4
    Me.ListBox1.RowSource = .Name & "!" & PrtsRng.Address

End With


Thank you.
Divinedar (rep: 20) Jan 28, '23 at 12:50 pm
Divinedar.

Firstly we are in danger of confusing questions and answers here. Have you seen my Answer to your second question? If so, I assume ListBox2 of my example does what you want (now or less), right? 

Secondly, I imagine you only have a few sheets (certainly c.f. a typical parts list) so I'd suggest picking the sheet by a drop-down ComboBox (and I previously suggested giving those sheet names a common start, in anticpation to ease populating a selection).

What you're aiming at is possible but, with respect, you're asking extending your question. I think I've answered your question in principle (despite the fact you didn't add a file this time) so could we close off this question (and your other question) then start a new one (about picking between worksheets via a ComboBox and listing checked items) perhaps?
John_Ru (rep: 6142) Jan 28, '23 at 1:45 pm
Ok I'll work on the worksheet thing later.  In using your last workbook you attached using the two listboxes I'm getting an error.

This code works perfect until I try to incorporate your code:

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


When I add your code for the listboxes I get a "Run-time error '91 here:

With frmGunParts
...


If I comment your code out it works pefectly to get to the 2nd userform but the listboxes are not populating.  What am I doing wrong?  I feel I'm missing something.
Divinedar (rep: 20) Jan 28, '23 at 3:15 pm
Divinedar. It'sSaturday night here and I'm not prepared to repeat the stes you took to modify you/my code get the error!

Provide me with a file and I might work it out but we're not here to bug-fix!. My first reaction is to say you have the line:
frmGunParts.Show

which doesn't call my demo UserForm1 so have you correctly copied the Initialize code "behind" your form and the controls have the correct names?
John_Ru (rep: 6142) Jan 28, '23 at 3:33 pm
No problem.  I've tried to upload the file so many times and I don't see an option to upload the file with the exception of editing the question. Like I said I'm in no hurry for you to work on it so please just take your time I'm just thankful for the help.

The userform to copy the code behind may be my problem.  The first userfrom is the Customer Info userform and the second form is the Gun Order Form when I want the listboxes to populate.  I went over the code and corrected the first listbox and the second listbox. 

I have the code in the Gun Order userform but getting the error in the Customer Info userfrom.

Enjoy your weekend. Send me a response after the weekend.  Got other things to work on.  Thank you and please no pressure.
Divinedar (rep: 20) Jan 28, '23 at 3:54 pm
As I said recently, you can only add a file to your question and it must be an Excel file.

I'm now not sure if you're pursuing this Answer or the (better) answer to your follow-up question. I'd prefer it if you'd clarified that please.

Remember not to expect me to solve all your problems or wrote all your code- I'm just an unpaid volunteer. You have to do some of the running! 
John_Ru (rep: 6142) Jan 28, '23 at 4:58 pm
Divinedar

I've given you a fuller answer (with sheet selection by ComboBox) as a replacement Answer to your followup question Populate Combox then Listbox by selection.
John_Ru (rep: 6142) Jan 30, '23 at 8:52 am
Add to Discussion


Answer the Question

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