+ Reply to Thread
Results 1 to 11 of 11

can't go cDbl("200%") ?

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    can't go cDbl("200%") ?

    This should be simple, a "level 1 user question." I wanted to seed an input box with 200% like
    sVal = InputBox(...,200%)
    so that the input line says
    200%
    but the only way I came up with to get the % to display was to use a string, like
    sVal = InputBox(...,"200%")

    Note, InputBox returns a string (sVal) regardless. As that string goes, I don't care if the % is there or not in sVal; I can react appropriately. (And by the way, I don't think Format() improves anything here.)

    But if sVal is "200%" then dVal=cDbl(sVal) fails. Is there another way to
    1. Show the percent sign in the white area of the InputBox
    2. Get the value 2 or 200 to the Double variable dVal
    ...short of having to parse out the % sign from sVal? Maybe using InputBox(...,"200%") is not the best approach.

    ( And by the way, speaking of parsing, is there a VBA counterpart of Substitute? Say, for Application.Worksheetfunction.substitute(sVal,"%","") ?)
    Last edited by Oppressed1; 11-22-2017 at 05:32 PM.

  2. #2
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: can't go cDbl("200%") ?

    As you are finding I believe an input box returns a string data type. Having the % character in there forces this even when you try and convert it. Your best bet is to either force the user to enter a numeric value only (validate the input) or put into place a method to strip out the % sign and then adjust whats left to a numeric representation of the percentage.

    You could instead of an input box create a form and use a label with "%" in it next to a text field. If you enter a numeric value like "200" into CDbl() it should convert as expected (to 200 as a number), its the "%" preventing that from working.

    I think the VBA function equivalent to SUBSTITUTE() is REPLACE().

    EDIT: I was wrong, input box returns the entry and what variable you store it in determines the data type. You could use a dbl data type and create an error handler to ensure they enter a valid number. Alternatively you could use string/variant and convertit to double, but in that case you are more likely to parse the entry removing characters like % before converting to dbl instead of validating their input.
    Last edited by Zer0Cool; 11-22-2017 at 05:35 PM.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: can't go cDbl("200%") ?

    percent character:
    Please Login or Register  to view this content.
    maybe this way?
    Please Login or Register  to view this content.
    Last edited by mjr veverka; 11-22-2017 at 05:39 PM.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: can't go cDbl("200%") ?

    Please Login or Register  to view this content.
    That doesn't work because % is the VBA type declaration character for Integer. It is not an operator as in Excel.
    Entia non sunt multiplicanda sine necessitate

  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: can't go cDbl("200%") ?

    Quote Originally Posted by Zer0Cool View Post
    EDIT: I was wrong, input box returns the entry and what variable you store it in determines the data type.
    Inputbox does return a String as you initially stated. Assigning that to a typed variable will result in implicit coercion as is typically the case with VBA.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: can't go cDbl("200%") ?

    Sorry, porucha, that CDbl gives an error, no different than what I had.

    By the way, I did consider using format, so instead of valVal & Chr(37), using format(valval, "0%"). My quest is to achieve that end without having to parse away the "%" from the return value of InputBox, in order to assign it numerically.

    I think the answer is to use Evaluate(sval), unless someone has something better. (Though parsing in this case is not tragic, it just SEEMS like an admission of defeat )

    (Yep, shg, good point. I was hoping to avoid a "string seeding" of the inputbox - sort of like how in a cell the value can be internally numeric, but formatted to appear as 200%. I was hoping that there was some way to get inputbox to display it the way I wanted, but without returning the %, since that is unfriendly to cDbl. BTW I'm really surprised that cDbl choked on that. It's usually very strong in interpreting strings as numbers.)

    Again, I think the answer is to use Evaluate(sval), unless someone has something better

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: can't go cDbl("200%") ?

    I was hoping to avoid a "string seeding" of the inputbox
    The Default argument is a string. If you use a number, VBA has to coerce it to a string.

    Textbox values are strings; there is no other underlying value.

  8. #8
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: can't go cDbl("200%") ?

    Quote Originally Posted by Oppressed1 View Post
    ...that CDbl gives an error, ...
    You have a strange excel who doesn't like 'CDbl':
    Attached Files Attached Files
    Last edited by mjr veverka; 11-22-2017 at 06:51 PM.

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: can't go cDbl("200%") ?

    Yeah shg, I was hoping you or someone had some clever way or "backdoor" to beat that, since cDbl is being such a brat about the % character.

    I'm going with Evaluate and marking solved. Short of parsing off the "%" from the return value, I see no other direct way of displaying a percent in an InputBox and getting the response into a numeric variable.

    The Form was a cool alternative, though, ZerCool. And you're right, Replace() was the function I couldn't remember.
    Last edited by Oppressed1; 11-24-2017 at 04:22 AM.

  10. #10
    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: can't go cDbl("200%") ?

    Wouldn't using Application.InputBox be easier?
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: can't go cDbl("200%") ?

    THAT'S what I was looking for! The nitwit hits yet another home run! Thank you once again.

    If only I could remember that "application form." I even bookmarked this less than a year ago but like some things you discover and bookmark, just never can remember it again!
    https://www.wiseowl.co.uk/blog/s2458/inputbox.htm
    (a really easy explanation of application.inputbox)
    Last edited by Oppressed1; 11-24-2017 at 04:22 AM.

+ 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. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. Replies: 2
    Last Post: 07-24-2017, 02:19 AM
  3. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  6. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  7. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

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