+ Reply to Thread
Results 1 to 9 of 9

VBA Userform Issue - Command Button / msg Box

  1. #1
    Registered User
    Join Date
    01-07-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    VBA Userform Issue - Command Button / msg Box

    I have a command button that when I click drops the contents of numerous list boxes into excel. I wanted to add a message box to tell the user when they have not selected an item in one of the list boxes. They need to select at least one item in each list box. Example below. It is not working, even if I select an item in each list box I still get the "Please select one question!" message box displayed rather than a successful submission. I tried and if then else statement but it did the same. need it to submit the data if items selected in all list boxes, message box if not all items selected. Any help much appreciated. Code i'm trying is;

    Please Login or Register  to view this content.
    Last edited by teeks; 01-07-2010 at 09:51 AM. Reason: did not add code tags. hope I have done this correctly now. Apologies

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA Userform Issue - Command Button / msg Box

    The problem is you are testing for False on selected items. But the selected property is a array of True/False elements, one for each item. So even if one is selected all the others will be false.

    This examples uses a function to determine whether an item has been selected or not.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA Userform Issue - Command Button / msg Box

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Forgot to include this. Can you edit your post please.

  4. #4
    Registered User
    Join Date
    01-07-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA Userform Issue - Command Button / msg Box

    Thanks for the code, how would it look for 2 list boxes on the userform, both of which have to have an item selected and both of which are submitting data to excel at the time of user form command button click

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Post Re: VBA Userform Issue - Command Button / msg Box

    Thanks for the code tag edit.

    You can use the same function. But we need to change the test logic.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-07-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA Userform Issue - Command Button / msg Box

    I'm getting in a right pickle with this now. I thought i'd post the full messy code. I'm getting the same problem again that the message box keeps reappearing. Once a MsgBox "Please select..." appears I want to return to the user form. Bitten off far more than I can chew, thanks for the help so far Andy!!

    Please Login or Register  to view this content.

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA Userform Issue - Command Button / msg Box

    I can not see a reason for getting msgboxes other than if no items are selected for a particular listbox.

    That said I also don't see in your code where the specific List items is obtained.

    So for example if Listbox1 has the last item selected your code will output what exactly? What is lItem

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-07-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA Userform Issue - Command Button / msg Box

    (litem) was a left over from the original code.

    I've now got the following code, based on your advice, it tells me ethnicity (listbox4) is not selected even when a option from the list box is Arrrghhhh!! Surprise, surprise, I can't understand why!

    Any thoughts. Thanks for all your help so far!

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA Userform Issue - Command Button / msg Box

    Hard to say without actually having the workbook.

    Sure listbox4 is what you think it is?
    Maybe the selected item is not visible, are all items visible or can you scroll the list?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1