+ Reply to Thread
Results 1 to 5 of 5

How to extract numbers for calculation when entering a string of text into a cell

Hybrid View

choletseng How to extract numbers for... 02-23-2024, 08:08 AM
JohnTopley Re: How to extract numbers... 02-23-2024, 08:30 AM
jindon Re: How to extract numbers... 02-23-2024, 08:31 AM
Special-K Re: How to extract numbers... 02-23-2024, 08:36 AM
choletseng Re: How to extract numbers... 02-23-2024, 08:40 AM
  1. #1
    Forum Contributor
    Join Date
    10-02-2021
    Location
    Thailand
    MS-Off Ver
    365
    Posts
    192

    How to extract numbers for calculation when entering a string of text into a cell

    Function calPP(inputStr As String) As Double
        Dim dimensions() As String
        dimensions = Split(inputStr, "*")
        If UBound(dimensions) < 2 Then
            MsgBox "Input format error, please make sure the input contains 3 numbers separated by '*'."
            B1 = 0
            Exit Function
        End If
        Dim od As Double, id As Double, ln As Double
        od = CDbl(Replace(dimensions(0), "mm", ""))
        id = CDbl(Replace(dimensions(1), "mm", ""))
        ln = CDbl(Replace(dimensions(2), "mm", ""))
        Dim volume As Double, mass As Double
        volume = (od - id) * ln * 8.96
        mass = volume / 1000
       calPP = mass
    End Function

    Why can't the code get results


    Cell A1 Key in OD9.525*0.8mm*850mmL

    formula like this ((9.525-0.8)*850*8.96)/1000

    Cell B1 Get result is 66.4496
    Last edited by choletseng; 02-23-2024 at 08:41 AM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,720

    Re: How to extract numbers for calculation when entering a string of text into a cell

    You need to remove "OD" from first dimension and "L" from units of last dimension.

    Use REPLACE if these are generic values.

    Temporary code:

    Function calPP(inputStr As String) As Double
        Dim dimensions() As String
        dimensions = Split(inputStr, "*")
        If UBound(dimensions) < 2 Then
            MsgBox "Input format error, please make sure the input contains 3 numbers separated by '*'."
            B1 = 0
            Exit Function
        End If
        Dim od As Double, id As Double, ln As Double
        od = CDbl(Replace(Right(dimensions(0), Len(dimensions(0)) - 2), "mm", ""))
        id = CDbl(Replace(dimensions(1), "mm", ""))
        ln = CDbl(Replace(dimensions(2), "mmL", ""))
        Dim volume As Double, mass As Double
        volume = (od - id) * ln * 8.96
        mass = volume / 1000
       calPP = mass
    End Function
    Attached Files Attached Files
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

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

    Re: How to extract numbers for calculation when entering a string of text into a cell

    Try change
        Dim od As Double, id As Double, ln As Double
        od = CDbl(Replace(dimensions(0), "mm", ""))
        id = CDbl(Replace(dimensions(1), "mm", ""))
        ln = CDbl(Replace(dimensions(2), "mm", ""))
    to
        Dim od As Double, id As Double, ln As Double
        od = Val(Mid$(dimensions(0), 3))
        id = Val(dimensions(1))
        ln = Val(dimensions(2))
    With Regular Expression
    Function calPP(inputStr As String) As Double
        With CreateObject("VBScript.RegExp")
            .Pattern = "\D*(\d+(\.\d+)?)\*(\d+(\.\d+)?)mm\*(\d+(\.\d+)?)mmL"
            If .test(inputStr) Then
                calPP = Evaluate(.Replace(inputStr, "(($1-$3)*$5*8.96)/1000"))
            End If
        End With
    End Function

  4. #4
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,077

    Re: How to extract numbers for calculation when entering a string of text into a cell

    Remove "OD" and the letter "L" from cell A1
    or insert "L" into your formula for ln.

    If you do that you get the correct result
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  5. #5
    Forum Contributor
    Join Date
    10-02-2021
    Location
    Thailand
    MS-Off Ver
    365
    Posts
    192

    Re: How to extract numbers for calculation when entering a string of text into a cell

    Wow, you two are so amazing. The problem has finally been successfully solved. Thank you both

+ 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] Extract numbers from text string
    By Delta729 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-18-2018, 04:54 PM
  2. [SOLVED] Extract numbers from text string
    By cmb80 in forum Excel General
    Replies: 3
    Last Post: 05-22-2017, 05:17 AM
  3. Extract Several Numbers From Text String
    By mvl3589 in forum Excel General
    Replies: 5
    Last Post: 06-21-2016, 08:11 PM
  4. [SOLVED] Extract numbers from text string
    By reynoldslarry in forum Excel General
    Replies: 6
    Last Post: 10-25-2014, 03:22 PM
  5. extract and sum numbers from a text string
    By Shazwb in forum Excel General
    Replies: 5
    Last Post: 08-28-2011, 09:24 PM
  6. Extract numbers from text string
    By mkvassh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2009, 08:53 AM
  7. EXTRACT NUMBERS FROM TEXT STRING
    By fiber_doc in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-28-2005, 02:45 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