+ Reply to Thread
Results 1 to 15 of 15

Convet string to double after replace function

  1. #1
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Convet string to double after replace function

    Please Login or Register  to view this content.
    Tried to convert a string to a number after replace. Why isnt't that working? The type Name of T_01.Value is still string and not double...

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: Convet string to double after replace function

    The contents of a textbox are always text. You have to use variables, or use the CDbl when passing the arguments to the Average function.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    Okey, this code now takes the average of my values. But it should ignore empty cells, which it does not... I thought that WorksheetFunction.Average ignores empty cells. From my point of view there is no chance to use WorksheetFunction.AverageIf, because I cannot specify my values into a range...right?


    Please Login or Register  to view this content.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    WorksheetFunction does ignore blank "Cells" and does not behave in same manner for zero, which I suspect one of your variable holds.

    I'd recommend doing Debug.Print of variables to check what value is held before the calculation (or use Locals Window/Watch Window to track).

    How you should handle it, will depend on your desired result in specific use case.

    I'd recommend uploading sample workbook which clearly demonstrate your issue with desired result for specific cases, if you need further help.
    Last edited by CK76; 01-31-2019 at 09:57 AM.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  5. #5
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    Start userform "Aktualisieren_Heizung"
    Click in the list box, take number 13
    After the pick press "Wärmebedarf" --> the next userform will pop up.
    --> the values from the database are printed out into the text boxes..
    The 12 text boxes at the top show yearly values, the three values below show averages.
    The first lower text box shows the average of "Heizwärmnebedarf" of 2015, 2016,2017, 2018. --> Thats where Im working on right now.. you can baically ignore the other boxes.

    The change events are in there because changes of the text boxes T_01 ... T_04 wil have an effect on the average.

    In the code you can ignore the the code, where a new entry is created... (new entries work)

    The text box on the very top left shows the row number your working on..

    Maybe this helps... I uncommented the change average events because error occurs.

    UserForm6 you can also ignore, its just there to make the code work...

    Best, Matthias



    Let me know if you cant navigate and find my problem, I will make another sheet then. The code where my problem lies is in "Heizung1" around the end.. I have already made a suggestion how to solve it, but don't know how to write it out to the end properly...:

    Something like that:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Not sure exactly where your issue is... but check LEN(Textbox.Value) and if it's = 0 then variable = null

    Checking LEN() of string is faster than checking if it is = to vbNullstring. Though in this instance, speed won't matter much.

    Ex:
    Please Login or Register  to view this content.
    Last edited by CK76; 01-31-2019 at 10:23 AM.

  7. #7
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    I tried the following:

    The goal is that if the T_01 Bo is empty the average function should ignore it.. Why isn't this working here? Is empty not equal to a empty cell in excel?


    [Copy to clipboard]

    Private Sub T_01_Change()
    T_01.Value = Replace(T_01.Value, ",", ".") ' Heizwärmebedarf Wärmeerzeuger 1 2015
    'If T_01.Value = "." Then
    'T_01a = Val(T_01.Value)

    If T_01.Value <> vbNullString Then
    T_01a = Val(T_01.Value)
    ElseIf T_01.Value = vbNullString Then
    T_01a = Empty
    End If



    T_02a = Val(T_02.Value)

    T_13 = WorksheetFunction.Average(T_01a, T_02a)

    T_14 = T_01.Value
    End Sub
    Last edited by travelkid; 01-31-2019 at 10:48 AM.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Use "Null" as I have shown in the example.

  9. #9
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    Its just not doing the job.

    Please Login or Register  to view this content.

    The IsNull() Test gives me back False..... so there is something wrong.
    Last edited by travelkid; 01-31-2019 at 11:18 AM.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Please Login or Register  to view this content.
    Above logic is the issue. Should be...
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    Hello, I have uploaded another worksheet, with what my question can be dealt better I think.

    Problem: You can pick a line from the list box and change the values in the text boxes, and then the mean should adapt to these changes. However it still does not ignore zeros... also I have uploaded pictures describing the problem
    Attached Images Attached Images
    Attached Files Attached Files

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Hmm, interesting... when I tested, null is only ignored if it is the LAST value in Application.Average(). Otherwise it's evaluated as zero. There must be some internal sorting logic going on for Average calculation when using Range object.

    Ex:
    Please Login or Register  to view this content.
    Returns 7.

    Where as...
    Please Login or Register  to view this content.
    Returns 4.666666...7

    I guess, it's easier to just write UDF to handle average calculation for your purpose. I'm going into a meeting now, will post UDF once I'm out.
    Last edited by CK76; 02-01-2019 at 11:42 AM.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Put this is standard module. Change UserForm1 as needed.
    Please Login or Register  to view this content.
    Then use it in your Userform1 like below.
    Please Login or Register  to view this content.
    Last edited by CK76; 02-01-2019 at 02:35 PM. Reason: Added small error handle in case x = 0

  14. #14
    Forum Contributor
    Join Date
    12-11-2018
    Location
    Zurich
    MS-Off Ver
    2016
    Posts
    130

    Re: Convet string to double after replace function

    OKey thanks, this is working. I have got a similar problem somwhere else in my project. The difference there is that, the names of my text boxes are T_01, T_05,T_07 and I would like to average them. How would this funciton look like then?
    Last edited by travelkid; 02-04-2019 at 08:21 AM.

  15. #15
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Convet string to double after replace function

    Loop through each control object in form then check for the name.
    Ex:
    Please Login or Register  to view this content.

+ 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. [SOLVED] vba Replace function not finding string
    By noblerot in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2020, 06:29 PM
  2. [SOLVED] VBA Replace Function at end of string only
    By Mullah in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-07-2018, 08:38 PM
  3. Specify a string within a replace function
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-06-2017, 09:46 PM
  4. [SOLVED] Using find replace to first find two double quotes and replace with a single double quote
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-17-2015, 10:13 AM
  5. [SOLVED] Function that takes in a string and returns a double
    By chipp300 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-16-2014, 06:56 PM
  6. [SOLVED] Need help with REPLACE function to replace HEX A0 with HEX 20 in string
    By flyboy54 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2013, 12:52 PM
  7. InStr Replace function problem - double entries
    By dominikniki123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2013, 08:47 AM

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