1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Any Excel expert here?

Discussion in 'BBS Hangout' started by freemaniam, Nov 28, 2008.

  1. freemaniam

    freemaniam 我是自由人

    Joined:
    Nov 18, 2005
    Messages:
    3,534
    Likes Received:
    337
    I am looking for a formula to sum up all the digit(s) within a range of cells, e.g.

    Code:
    CELL    VALUE
    ====    =====
    A1         8
    B1        21
    C1        15
    D1         8
    E1         3
    F1        91
    G1         4
    H1        14
    The formula will return 47 (8+2+1+1+5+8+3+9+1+4+1+4).

    I tried MID function but it is not working properly as the value within the cell could be either single digit or double digits.

    Anyone has a clue? Or do you know which forum I should go for the answer?

    Many thanks in advance.
     
  2. hotblooded

    hotblooded Member

    Joined:
    Sep 12, 2006
    Messages:
    1,346
    Likes Received:
    3
    I am not too sure but maybe trying to build a macro for it?
     
  3. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,994
    Likes Received:
    34,243
  4. hotblooded

    hotblooded Member

    Joined:
    Sep 12, 2006
    Messages:
    1,346
    Likes Received:
    3
  5. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Can you not do a SUM?
     
  6. Master Baiter

    Master Baiter Member

    Joined:
    Jul 6, 2001
    Messages:
    9,608
    Likes Received:
    1,376
    That's what I would do. You could even hide the column with the data that you don't want to see.
     
  7. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,994
    Likes Received:
    34,243
    Yeah, after the link I posted, it would be easy to sum the results in another cell, but it seemed like he wanted to do it all in one cell, which I'm not sure about. I use Excel a lot, but usually not with a lot of crazy formulas. :)
     
  8. freemaniam

    freemaniam 我是自由人

    Joined:
    Nov 18, 2005
    Messages:
    3,534
    Likes Received:
    337
    Wow! You guys are great! Thanks a lot Dr of Dunk!

    The link is very useful. Actually it solves a major part of the problem I encountered. I will try to incorporate it into the final formula and see if it works.

    Thanks again guys, I will let you know the results.
     
  9. ScriboErgoSum

    ScriboErgoSum Member
    Supporting Member

    Joined:
    Aug 5, 2002
    Messages:
    3,150
    Likes Received:
    388
    I got curious about this and wrote up a quick macro to calculate it and put it in a target cell. Just fill in the first 6 values to suit your range.

    Code:
    Sub CalculateDigits()
        Dim ls_Worksheet As String
        Dim ls_TargetRangeStartColumn As String
        Dim ls_TargetRangeEndColumn As String
        Dim ls_TargetRangeStartRow As String
        Dim ls_TargetRangeEndRow As String
        Dim ls_DisplayResultRange As String
        
        Dim li_DigitIndex As Integer
        Dim li_ColumnIndex As Integer
        Dim li_ColumnIndexStart As Integer
        Dim li_ColumnIndexEnd As Integer
        Dim li_RowIndex As Integer
        Dim li_RowIndexStart As Integer
        Dim li_RowIndexEnd As Integer
        Dim li_RowCount As Integer
        Dim li_ColumnCount As Integer
        
        Dim li_Length As Integer
        Dim ll_Result As Long
        Dim li_Row As Integer
        Dim li_Column As Integer
        Dim ls_CellValue As String
        
        '***********************************************
        '***    Set up Values here
        '***    These values calculate sum of digits
        '***    for cells C3:T100 on Sheet1
        '***
        '***    The Result is put in Cell AA1
        '***********************************************
        
        ls_Worksheet = "Sheet1" 'Paste Name of Worksheet here
        ls_TargetRangeStartColumn = "C"
        ls_TargetRangeStartRow = "3"
        ls_TargetRangeEndColumn = "T"
        ls_TargetRangeEndRow = "100"
        ls_DisplayResultRange = "AA1"
        ls_TargetRangeStartColumn = UCase(ls_TargetRangeStartColumn)
        ls_TargetRangeEndColumn = UCase(ls_TargetRangeEndColumn)
        
        If (Len(ls_TargetRangeStartColumn) = 1) Then
            li_ColumnIndexStart = Asc(ls_TargetRangeStartColumn) - 64
        Else
            li_ColumnIndexStart = Asc(Mid(ls_TargetRangeStartColumn, 2, 1)) - 64
            li_ColumnIndexStart = li_ColumnIndexStart + (Asc(Mid(ls_TargetRangeStartColumn, 1, 1)) - 64) * 26
        End If
        
        If (Len(ls_TargetRangeEndColumn) = 1) Then
            li_ColumnIndexEnd = Asc(ls_TargetRangeEndColumn) - 64
        Else
            li_ColumnIndexEnd = Asc(Mid(ls_TargetRangeEndColumn, 2, 1)) - 64
            li_ColumnIndexEnd = li_ColumnIndexEnd + (Asc(Mid(ls_TargetRangeEndColumn, 1, 1)) - 64) * 26
        End If
        
        li_ColumnCount = li_ColumnIndexEnd - li_ColumnIndexStart + 1
        
        li_RowIndexStart = CInt(ls_TargetRangeStartRow)
        li_RowIndexEnd = CInt(ls_TargetRangeEndRow)
        li_RowCount = li_RowIndexEnd - li_RowIndexStart + 1
        
        ll_Result = 0
        li_ColumnIndex = 1
        li_Column = 1
        
        Do While (li_ColumnIndex <= li_ColumnCount)
            li_RowIndex = 1
            Do While (li_RowIndex <= li_RowCount)
                ls_CellValue = CStr(Sheets(ls_Worksheet).Cells(li_RowIndex + li_RowIndexStart - 1, li_ColumnIndex + li_ColumnIndexStart - 1).Value)
                li_Length = Len(ls_CellValue)
                li_DigitIndex = 1
                Do While (li_DigitIndex <= li_Length)
                    ls_Temp = Mid(ls_CellValue, li_DigitIndex, 1)
                    ll_Result = ll_Result + CInt(Mid(ls_CellValue, li_DigitIndex, 1))
                    li_DigitIndex = li_DigitIndex + 1
                Loop
                li_RowIndex = li_RowIndex + 1
            Loop
            li_ColumnIndex = li_ColumnIndex + 1
        Loop
        
        Sheets(ls_Worksheet).Range(ls_DisplayResultRange).Value = ll_Result
    End Sub
    
     
  10. ScriboErgoSum

    ScriboErgoSum Member
    Supporting Member

    Joined:
    Aug 5, 2002
    Messages:
    3,150
    Likes Received:
    388
    I should have noted that the macro above will handle numbers of any length (1 digit or 20+ digits). Hopefully that helps. :)
     
  11. Fatty FatBastard

    Joined:
    Jul 13, 2001
    Messages:
    15,916
    Likes Received:
    159
    Is there something better than EXCEL now?

    I've always loathed a lot of things on there.

    Mainly on how to make it presentable, more than the math.
     
  12. hotblooded

    hotblooded Member

    Joined:
    Sep 12, 2006
    Messages:
    1,346
    Likes Received:
    3
    :eek: I just did a VBA crash course...but nothing like this

    Did you record macros and then adjusted it or did you do it from scratch?
     
  13. JeopardE

    JeopardE Member

    Joined:
    Jun 29, 2006
    Messages:
    7,418
    Likes Received:
    247
    But there is always a simpler way ....

    The formula below will work for numbers up to six digits. If you need more digits just add more quotient functions to the top accordingly. B5 of course is the cell you're trying to add up. And it will fill nicely too, so you can put these formulas in a column and then sum them up.

    =QUOTIENT(MOD(B5,1000000),100000)
    +QUOTIENT(MOD(B5,100000),10000)
    +QUOTIENT(MOD(B5,10000),1000)
    +QUOTIENT(MOD(B5,1000),100)
    +QUOTIENT(MOD(B5,100),10)
    +MOD(B5,10)
     
  14. JeopardE

    JeopardE Member

    Joined:
    Jun 29, 2006
    Messages:
    7,418
    Likes Received:
    247
    Follow-up -- this problem is clearly an integer division problem (which is not readily apparent if you're used to VB programming), hence the solution above.
     
  15. ScriboErgoSum

    ScriboErgoSum Member
    Supporting Member

    Joined:
    Aug 5, 2002
    Messages:
    3,150
    Likes Received:
    388
    From scratch. I've written a lot of macros. It's just a triple nested loop with a bit of extra code to figure out how to parse the column letters into numbers.

    FFB, Excel is the bomb. If you know how to use it, you can make it look good and accomplish just about anything. I use it for a pretty wide variety of things, including creating custom Word files, generating custom email newsletters, and database applications.
     
  16. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,994
    Likes Received:
    34,243
    Another reason why crash courses suck! There's nothing crazy in what he did (nice job, btw!). The only thing you'd maybe have to look up would be how to access a specific cell/worksheet. Get your money back! :D
     
  17. freemaniam

    freemaniam 我是自由人

    Joined:
    Nov 18, 2005
    Messages:
    3,534
    Likes Received:
    337
    You guys are simply awesome!

    ScriboErgoSum, that's great effort, I will definitely try. I am novice on macro and vba though.

    Honestly, I cannot incorporate the previous formula into the final one I need, shame on me. I will keep trying to resolve the problem myself. If I eventually stuck in the dead end again, I will seek help from all of you again. If I finally got it resolved, I will post the original task here then see if any of you are interested to it.

    Thank you all again.
     
  18. uskdew08

    uskdew08 New Member

    Joined:
    Nov 28, 2008
    Messages:
    2
    Likes Received:
    0
    Another option you have is entering the following as an array formula (you have to hold down the Ctrl & Shift keys when you press enter)

    =SUM(IF(LEN(A1:H1)>1,LEFT(A1:H1)+RIGHT(A1:H1),A1:H1))

    You will know that you have done it right if you see brackets at the beginning and end of the formula after it has been entered:

    {=SUM(IF(LEN(A1:H1)>1,LEFT(A1:H1)+RIGHT(A1:H1),A1:H1))}
     
  19. uskdew08

    uskdew08 New Member

    Joined:
    Nov 28, 2008
    Messages:
    2
    Likes Received:
    0
    The formula I posted will work up to 2 digits.
     
  20. hotblooded

    hotblooded Member

    Joined:
    Sep 12, 2006
    Messages:
    1,346
    Likes Received:
    3

    Haha it was free training from work, so no harm done, plus I like free lunches.

    I was referring to the time he spent writing that macro. But to a pro, it will probably only take 30 minutes?
     

Share This Page