+ Reply to Thread
Results 1 to 7 of 7

Summing Numeric Digits in a String

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-21-2021
    Location
    Norfolk, UK
    MS-Off Ver
    Excel 2003, 2007, 2013, 2019
    Posts
    215

    Summing Numeric Digits in a String

    Twenty years ago I built the following Function.

    Function SUMDIGITS(MyString As String) As Long
    Dim MyLen As Long
    Dim MyAnswer As Long
    Dim i As Integer
    
    
    MyAnswer = 0
    MyLen = Len(MyString)
    
    For i = 1 To MyLen
        If IsNumeric(Mid(MyString, i, 1)) Then
            MyAnswer = MyAnswer + CInt(Mid(MyString, i, 1))
        End If
    Next
    
    SUMDIGITS = MyAnswer
          
    End Function
    It is gives strange answers. For example applying this function to the string

    // ref1 & ref2 may be 6- or 8-digit references eg SU387148 or SU38714856
    Generates the answer 90, which is the sum of each individual digit in the string.

    Can anyone identify a good reason for keeping the Function as it stands?
    Regards,

    Stephen

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

    Re: Summing Numeric Digits in a String

    How is that a strange answer? The functions does exactly what you described. Surely the question is why would you use it on that string?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    07-21-2021
    Location
    Norfolk, UK
    MS-Off Ver
    Excel 2003, 2007, 2013, 2019
    Posts
    215

    Re: Summing Numeric Digits in a String

    Quote Originally Posted by rorya View Post
    Surely the question is why would you use it on that string?
    I agree with you.

  4. #4
    Forum Contributor
    Join Date
    07-21-2021
    Location
    Norfolk, UK
    MS-Off Ver
    Excel 2003, 2007, 2013, 2019
    Posts
    215

    Re: Summing Numeric Digits in a String

    My thanks to everyone who has taken the trouble to look at this thread.

    Following Rory's interpretation, no one has thought of a good reason for using the Function.

    Since this mirrors my own view, I shall set this thread to 'SOLVED'.

  5. #5
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Summing Numeric Digits in a String

    What did/do you want it to do????
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

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

    Re: Summing Numeric Digits in a String

    I can think of cases where it would be useful, I just said I couldn't see the point of using it on text like that!

  7. #7
    Forum Contributor
    Join Date
    07-21-2021
    Location
    Norfolk, UK
    MS-Off Ver
    Excel 2003, 2007, 2013, 2019
    Posts
    215

    Re: Summing Numeric Digits in a String

    Rory and Glenn. At the time (20 years ago), I was just playing with how to extract number values from strings. I hoped I might find a use for it but nothing has appeared

    Essentially, this was the beginning of journey which lead me to extract arrays / databases of information about numeric values contained within text strings. This was, potentially, much more useful, but limited to fairly short strings - say less than a 1000 characters.

    Typically other things interfered and I lost sight of the project. I am only now looking again at it.

    Rory as it stands the SUNDIGITS function only the operates on strings, but there is also a sister function NUMDIGITS.
    Last edited by swaatacba; 09-14-2023 at 01:00 PM.

+ 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: 2
    Last Post: 08-15-2023, 02:17 AM
  2. Count numeric digits in the cell formula string!
    By Mohammad Munawar in forum Excel General
    Replies: 1
    Last Post: 09-09-2022, 04:10 AM
  3. Replies: 14
    Last Post: 03-08-2014, 03:25 PM
  4. Using the IF function with non-numeric digits & with numeric consequences
    By oldmillbill in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-08-2012, 02:39 PM
  5. Replies: 11
    Last Post: 11-16-2011, 12:56 PM
  6. Extracting Numeric Values from an Alpha/Numeric String
    By Delkath in forum Excel General
    Replies: 5
    Last Post: 10-27-2010, 02:36 PM
  7. [SOLVED] Summing part of an Alpha Numeric String
    By Arturo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-23-2005, 06:06 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