+ Reply to Thread
Results 1 to 18 of 18

How to treat data in listbox as VALUES

  1. #1
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    How to treat data in listbox as VALUES

    Hello.

    Please, do you know if Listbox can contain also data formated as numbers ? I read somewhere that Listbox in useform is only for text and not numbers. Thus despite I can add to listbox numbers, their format is not NUMBER but TEXT. I just want to use my numbers in listbox as filter criteria. So user select from listbox, let's say 5, and my column will be sorted only on cells containing number 5. I can use this method for TEXTs (strings), but for numbers, it's not working. Excel somehow treat with numbers in litsbox as text. Basically It's working, but only when I setup format of data in my filtering column as GENERAL. But when I want to setup number format of related cells to let's say "number with 2 decimals", then it's not working.
    I tried to change data type of my listbox value, like: Val(my_userform.listbox1.value) or Cdec(my_userform.listbox1.value) or
    Cint(my_userform.listbox1.value) , but no success. It's like I can't change format of data in listbox to NUMBER.

    If you have some idea how can be data in listbox changed to NUMBER format, so I can work with it further (in my case to setup as criteria to filter column containing numbers), I would be very thankful to you. When I write it to textbox, it's working well, but not in Listbox.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to treat data in listbox as VALUES

    If you want to convert text to numbers take a look at the conversion functions that VBA provides.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to treat data in listbox as VALUES

    Not sure to see exactly the issue in the file attached the value selected is a number.
    Can you send a sample of your file
    Attached Files Attached Files
    - Battle without fear gives no glory - Just try

  4. #4
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Hello.

    thank you for your advices. Here I am attaching simply file which shows my issue quite good. If you click on button "filter data", you will see listbox, which is filled by data in column A. My problem is following:
    - when I setup number format of data in column A, let's say on two decimals etc, when I click on button in useform for filtering, nothing is found.
    - but when format of data in column A is general, it works ! But only when I don't use any converting function in my code. Currently I tried to modify data in my listbox in following way >>
    Please Login or Register  to view this content.
    So if I would omit function CDbl and format and set data in column A as General, my filter wokrs, otherwise not.

    My assuption is that user will change format in column A to NUMBER, 2 decimals.

    Macro for adding data to Listbox
    Please Login or Register  to view this content.
    Macro for processing filter based on selection in listbox:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to treat data in listbox as VALUES

    Not sure to have understood the issue:
    See attached your file with 2 demonstrations shown by some screenshots in sheet "demo"
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to treat data in listbox as VALUES

    Replace the codes in UserForm with the following codes.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Thank you very much guys. I will go through your solutions and will get back here, in case I will be still in doubts. I see there some new commands and functions you used which I don't know so far.
    Thanks again for your help !

  8. #8
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Hi guys, I still don't have it :-( Jindon, I copied your codes but it's not working:-( Maybe I do something wrong.
    I really don't understand it. In attachment from PCI is visible that number format is following 0.00 . When I try to change number format to 0.00, it always put instead of dot (0.00) comma (0,00). But even more strange thing is , that I placed check into my code >>> for example : listbox.list(2) = cells(2,1).value .... and it shows, that YES, it's the same. Then WHY automatic filter can't find this value ?

    Basically I still think that this is not rocket sience. There is no need of special conversion of data when loading to listbox from certain cells. If data in cells are numbers, then automatically Excel retain them as number also in Listbox, right ? . Also no need to setup special format like "0.00" etc, right ? Because 18 is the same as 18.00 or 18.00000. And despite I change number format in listbox which is not the same as in cells, it shouldn't be issue, right ? As stated above, 18 = 18.00
    But it seems somehow VBA must have exactly the same number format in listbox as it's in certain cells.

    Please, does somebody of you have some small macro which use listbox for filtering data, especially NUMBERS ?

    thanks a lot.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to treat data in listbox as VALUES

    See..............
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Hi Jindon.
    I am sorry, but it doesn't work. Can't this be connected with fact, that you have Excel 2010 and me 2007 ?
    This is what I see when I click on button "filter data" in your file >> 0, 1, 3, 5, 7 etc. They don't have decimals...I supposethis is result of Val function and can be easily changed to using format function.

    But still, problem is, when I select some option in listbox and click on "filter" button, filter can't find this value(s). If you can work with that, it's most probably Excel version issue. Or maybe some settings, but I doubt.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to treat data in listbox as VALUES

    Really strange to me.
    It is working fine.
    try this
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    thank you Jindon for your time, I appreciate it. Don't spend more time on this stupid thing.
    I also don't know where is problem. Below You can see attachment. It's your file, i just added 2 screenshots into 2. tab - so you can see what I see. Whatever option i select in listbox, filter doesn't work. Maybe I have something wrong set in my excel....in settings, if you can work with it and me not, where else can be problem (excel Excel 2007-2010 version) ?

    I thought that problem is that items selected in listbox and added to dynamic array x() are strings. But then I did a check, like x(0) + x(1) ...and Excel did this calculation correctly, so in array x() there were really numbers, but still when I wanted to apply selections in filter, no success. I also updated array data type from string to Variant or Double, but no success. Sometimes filter works, but ONLY when there is exactly the same number of decimal places in cell and in listbox....for me it's a sign, that item in listbox is formatted as string.

    Maybe it's a time to let it be and do something else, maybe sleep :-))

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to treat data in listbox as VALUES

    No,

    It if doesn't work at your side, your EXCEL should have problem.

  14. #14
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Yes, right, probably it's because of Excel. But don't you think it's just because you have higher Excel version than me ? I tested my and your macro on different computer, where is Excel 2007 proffesional and still the same problem.
    Maybe this sort of thing is in excel 2010 better covered than in 2007. In my eyes, values in listbox are still somehow taken as string and not numbers regardless how many functions I use to force Excel to convert it (Val, format, cdbl etc.) - at least in Excel 2007. And thus Excel can't find number in listbox in specified range of data.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to treat data in listbox as VALUES

    All the Value from controls should be STRING. This is the fact.

    When you filter the column with Array, criteria should match exactly the same as how it is shown in the cell.

    2 will not match to 2.00 etc, so you should not apply Val function to convert it to a double.
    Just run the code so you should understand what I'm saying
    Please Login or Register  to view this content.
    And this functionality is intrrodeuced from Excel 2007.

  16. #16
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: How to treat data in listbox as VALUES

    Hi Rudo123.
    seems it works (using Jindon's code)
    try

  17. #17
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    Thank you Jindon, nilem.
    I really opened your files, didn't change anything and can't use that filter. So it must be connected with Excel version. I will try to instal today trial Excel (office) 2010 and see what happens. I will let you know for sure.

  18. #18
    Registered User
    Join Date
    05-18-2015
    Location
    Bratislava
    MS-Off Ver
    Office 2007
    Posts
    33

    Re: How to treat data in listbox as VALUES

    HALELUJA guys !! :-)
    During my installation of MSO 2010 I was browsing net and trying to find some useful information. I found something saying that if somebody wants to see in listbox exactly the same as stated in some cell, he/she needs to use this command : cells(1,1).text

    So instead of writing : UserForm1.ListBox1.AddItem Worksheets("data1").Cells(j, 1).Value , I wrote UserForm1.ListBox1.AddItem Worksheets("data1").Cells(j, 1).Text and then it was working. I didn't have to use any functions like Val, Format etc.
    I hope there is no hidden problem in this solution. If no, then it would mean that it's enough to setup desired format in cell range and using "text" command the same format is shown in listbox.

    I installed trial MSO 2010 version now, so will try to play with your macros/solutions. I really couldn't run it under MSO 2007.

    Thanks a lot again for all your help, ideas. I learnt a lot ! Have a good weekend.

+ 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. Make Excel charts treat certain values as =na()
    By davidx in forum Excel General
    Replies: 5
    Last Post: 06-20-2015, 04:49 AM
  2. 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
  3. [SOLVED] How lines in combination charts treat #N/A values
    By Bernard Harris in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-16-2006, 12:20 AM
  4. [SOLVED] How to make excel to treat values in cell as a number?
    By Peri in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2005, 06:45 PM
  5. Excel - treat pairs of figures differently according to values
    By Alistair in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-10-2005, 06:06 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