+ Reply to Thread
Results 1 to 8 of 8

Cannot get value from ListBox in UserForm

  1. #1
    Registered User
    Join Date
    09-20-2016
    Location
    Hong Kong
    MS-Off Ver
    2013
    Posts
    4

    Cannot get value from ListBox in UserForm

    Hello,

    Below are my codes I typed in Excel Macro.

    Please Login or Register  to view this content.
    I created 4 ListBoxs in a UserForm, then I added years into ListBox1 and ListBox3, and months into ListBox2 and ListBox 4. After that, I set the defalut values to be today year and month repectively, and there is a button called "Calinterest" to get the values from the ListBoxs. But it seems the program cannot get the ListBox4's value if I just click the button without clicking a value in the ListBox4, even if the value in ListBox 4 is highlighted, it always returns nothing. The program works fine if I click all the ListBoxs. Is there something wrong when I set the default value for the ListBoxs?

    Thanks,
    Howard
    Last edited by howard1324; 05-23-2017 at 10:09 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Cannot get value from ListBox in UserForm

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Cannot get value from ListBox in UserForm

    Please Login or Register  to view this content.
    When you declare variables like above, only em is type Integer. All the other variables are the default variable type, Variant.



    Please Login or Register  to view this content.
    This line is trying to compare em to a nullstring "".
    Because em is an Integer variable, it cannot be compared to a string.
    The other variable comparisons are okay because they are Variants.



    Declare each variable as type String like this and it should work.
    Please Login or Register  to view this content.
    If later in the code you want to use the variable as a number, wrap the variable in the Val() function e.g.; Val(em)
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    09-20-2016
    Location
    Hong Kong
    MS-Off Ver
    2013
    Posts
    4

    Re: Cannot get value from ListBox in UserForm

    Hi AlphaFrog, thank you for your reply.

    First, we can declare multiple variables by specifying the variable name and separate them by commas. So I don't think "only em is type Integer".

    Second, if there is no value got from the ListBox4, em would return "". I know there was no value returned by ListBox4 because I ran the program code line by line and add "watch" to the variables.

    Lastly, I have tried to declare each variable as type String as you suggested. But I still cannot get the value for em from ListBox4.

    The main problem here is I cannot get the default value. It returns nothing for ListBox4.
    Please Login or Register  to view this content.
    If I remove these four lines, the program works fine. But I want to know why it cannot get the ListBox4 value when I assigned one for it.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Cannot get value from ListBox in UserForm

    Quote Originally Posted by howard1324 View Post
    First, we can declare multiple variables by specifying the variable name and separate them by commas. So I don't think "only em is type Integer".
    Hi,

    Yes you can declare them like that, but unless you specify the type for each one, all but the last one will be Variants. AlphaFrog is entirely correct.

    For your listbox issue, I think you are running into a bug here. Perhaps use
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Cannot get value from ListBox in UserForm

    Quote Originally Posted by howard1324 View Post
    First, we can declare multiple variables by specifying the variable name and separate them by commas. So I don't think "only em is type Integer".
    Look at the Locals window as you step trough the code and you will see the variables listed and their type. If you declare them your way, only em will be type Integer.


    Second, if there is no value got from the ListBox4, em would return "". I know there was no value returned by ListBox4 because I ran the program code line by line and add "watch" to the variables.
    Agreed, if there is no value returned by Listbox4 the it returns a nullstring "". ListBoxes return strings. That's why I suggested you declare each variable as a string.


    Lastly, I have tried to declare each variable as type String as you suggested. But I still cannot get the value for em from ListBox4.

    The main problem here is I cannot get the default value. It returns nothing for ListBox4.
    Please Login or Register  to view this content.
    If I remove these four lines, the program works fine. But I want to know why it cannot get the ListBox4 value when I assigned one for it.
    If you did change the declaration for each variable to string as I suggested, I don't know why it doesn't work. I do know that if you declare em as Integer, then If em = "" will throw a type mismatch error.

    Perhaps double-check that your ListBox4 is actually named ListBox4 and not somethineg else.

  7. #7
    Registered User
    Join Date
    09-20-2016
    Location
    Hong Kong
    MS-Off Ver
    2013
    Posts
    4

    Re: Cannot get value from ListBox in UserForm

    Hello xlnitwit,
    Please Login or Register  to view this content.
    Thank you. It works.

  8. #8
    Registered User
    Join Date
    09-20-2016
    Location
    Hong Kong
    MS-Off Ver
    2013
    Posts
    4

    Re: Cannot get value from ListBox in UserForm

    Quote Originally Posted by AlphaFrog View Post
    If you did change the declaration for each variable to string as I suggested, I don't know why it doesn't work. I do know that if you declare em as Integer, then If em = "" will throw a type mismatch error.

    Perhaps double-check that your ListBox4 is actually named ListBox4 and not somethineg else.
    Thanks. I think it might be a bug.
    xlnitwit's solution works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Userform ListBox depending on a previous Listbox selection
    By Figolu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2015, 04:31 PM
  2. [SOLVED] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  3. [SOLVED] copy contents of userform listbox to another userform listbox
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2014, 11:39 AM
  4. Transfer Data From One Userform Listbox to Another Userform Listbox with 11 columns
    By sparkoft in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-18-2013, 10:54 AM
  5. userform listbox
    By tsiguy96 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-14-2013, 11:09 PM
  6. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  7. Replies: 1
    Last Post: 05-17-2006, 04:50 PM

Tags for this Thread

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