Add digits together

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good afternoon,

The code below adds together ALL the individual digits of each combination and lists those totals, it also lists the total combinations associated with each of those totals.

The Excel formula if the combination was in cells A1:F1 would be...

Code:
=SUMPRODUCT(INT(A1:F1/10)+MOD(A1:F1,10))

My question is, how can I adapt this code to add those totals together and list the total combinations associated with each of those totals.

For example, if the combination was 05, 44, 46, 48, 50, 52, then 5+4+4+4+6+4+8+5+5+2 = 47, and 47 would be 4+7 = 11.

I know that the minimum would be 2 and the maximum would be 15.

The Excel formula if the combination total was in cell A1 would be...

Code:
=INT(A1/10+MOD(A1,10))

I just can't seem to incorporate this into the existing code and get it to work.

Here is the code...

Code:
Option Explicit
Option Base 1
Const MinA As Integer = 1
Const MaxF As Integer = 59
    
Sub Root_Sum()
    Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
    Dim Num As Long
    Dim nums(59) As Long
    Dim Root As Long
    Dim Map(11 To 76) As Double
    Dim n As Long
    Dim Total As Long
    With Application
        .ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
    End With
    Columns("A:B").ClearContents
    For Num = LBound(nums) To UBound(nums)
        nums(Num) = Num \ 10 + Num Mod 10
    Next Num
    For A = MinA To MaxF - 5
        For B = A + 1 To MaxF - 4
            For C = B + 1 To MaxF - 3
                For D = C + 1 To MaxF - 2
                    For E = D + 1 To MaxF - 1
                        For F = E + 1 To MaxF
                            Root = nums(A) + nums(B) + nums(C) + _
                                   nums(D) + nums(E) + nums(F)
                            Map(Root) = Map(Root) + 1
                        Next F
                    Next E
                Next D
            Next C
        Next B
    Next A
    For n = LBound(Map) To UBound(Map)
        Total = Total + Map(n)
        ActiveCell.Offset(n - LBound(Map), 0).Value = n
        ActiveCell.Offset(n - LBound(Map), 1).Value = Map(n)
    Next n
    ActiveCell.Offset(n - LBound(Map), 1).Value = Total
    With Application
        .DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
    End With
End Sub

Thanks very much in advance.
 
Would you mind posting your final code? Where do you use
Code:
=INT(existing total / 10 + MOD (existing total, 10))
 
Last edited:
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Would you mind posting your final code? Where do you use
Rich (BB code):
=INT(existing total / 10 + MOD (existing total, 10))

Hi Jack,

That was just an example to try and relay what I needed to do, I admit that I could have described that better, sorry for the confusion.

What I was trying to say was that I needed to take the existing totals of the initial digits being added together, like they do in the original code posted, and then add them together again, like it does in the piece of code...

Code:
    For Num = LBound(Digits) To UBound(Digits)
        Digits(Num) = Num \ 10 + Num Mod 10
    Next Num

Here is the amended code that works...

Code:
Option Explicit
Option Base 1

Const MinA As Integer = 1
Const MaxF As Integer = 59
    
Sub Root_Sum()
    Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
    Dim Num As Long
    Dim nums(59) As Long
    Dim Root As Long
    Dim Map(2 To 15) As Double ' [B]<--- Changed to accomodate the minimum & maximum values.[/B]
    Dim n As Long
    Dim Total As Long
    With Application
        .ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
    End With
    Columns("A:B").ClearContents
    For Num = LBound(nums) To UBound(nums)
        nums(Num) = Num \ 10 + Num Mod 10
    Next Num
    For A = MinA To MaxF - 5
        For B = A + 1 To MaxF - 4
            For C = B + 1 To MaxF - 3
                For D = C + 1 To MaxF - 2
                    For E = D + 1 To MaxF - 1
                        For F = E + 1 To MaxF
                            Root = nums(A) + nums(B) + nums(C) + _
                                   nums(D) + nums(E) + nums(F)
                            Map(Root \ 10 + Root Mod 10) = _
                                Map(Root \ 10 + Root Mod 10) + 1 ' [B]<--- Changed to further add digits together again.[/B]
                        Next F
                    Next E
                Next D
            Next C
        Next B
    Next A
    For n = LBound(Map) To UBound(Map)
        Total = Total + Map(n)
        ActiveCell.Offset(n - LBound(Map), 0).Value = n
        ActiveCell.Offset(n - LBound(Map), 1).Value = Map(n)
    Next n
    ActiveCell.Offset(n - LBound(Map), 1).Value = Total
    With Application
        .DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
    End With
End Sub

Thanks, I appreciate your time on this.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top