Help with VBA Code modification

togenmoser

New Member
Joined
Mar 21, 2024
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Hello, I want a little modification to the code please. I want excel to only find pairs with the value difference of (-1+1, -2+2, -3+3, -5+5, and -7+7) and sum up to the target value.
I will appreciate your help in modifying the code. Thanks in anticipation



VBA Code:
Sub FindAllSumPairs()
    'Declare variables
    Dim ws As Worksheet
    Dim rng As Range
    Dim i As Long, j As Long, k As Long, m As Long, l As Long
    Dim sum As Long, xx As Long, yy As Long, zz As Long
    Dim color As Long
    Dim prevColor As Long
    Dim diff As Double
    Dim criteria(1 To 5) As Long
    Dim pairValue1 As Long, pairValue2 As Long
    Dim criteriaMatch1 As Boolean, criteriaMatch2 As Boolean

    'Set worksheet and range
    Set ws = ActiveSheet
    Set rng = ws.UsedRange                       'Change this as needed

    'Get criteria values from B1 to F1
    For i = 1 To 5
        criteria(i) = ws.Cells(1, i + 1).Value
    Next i

    'Loop through the target values in H1 to Q1
    For i = 8 To 18
        'Get the target value and a random color
        sum = ws.Cells(1, i).Value
        color = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))

        'Check if the chosen color is too close to the previous color
        If i > 8 Then
            'Get the previous color
            prevColor = ws.Cells(1, i - 1).Interior.color

            'Calculate the difference between the two colors using Euclidean distance formula
            diff = Sqr(((color Mod 256) - (prevColor Mod 256)) ^ 2 + ((color \ 256 Mod 256) - (prevColor \ 256 Mod 256)) ^ 2 + ((color \ 65536) - (prevColor \ 65536)) ^ 2)

            'If the difference is less than a threshold, choose a different color
            If diff < 50 Then                    'Change this as needed
                color = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
            End If
        End If

        'Loop through the rows in the range
        For j = 2 To rng.Rows.Count              'Start from row 2 to avoid the header
            'Loop through the sets of 5 cells in the same row, separated by empty cells
            For m = 1 To rng.Columns.Count Step 6
                'Loop through the columns in the same set of 5 cells
                For k = m To m + 4
                    'Loop through the remaining columns in the same set of 5 cells
                    For l = k + 1 To m + 4
                        'Check if the current cell and another cell are not empty and add up to the target value
                        If Not IsEmpty(rng.Cells(j, k)) And Not IsEmpty(rng.Cells(j, l)) And rng.Cells(j, k).Value + rng.Cells(j, l).Value = sum Then
                            pairValue1 = rng.Cells(j, k).Value
                            pairValue2 = rng.Cells(j, l).Value

                            'Reset criteria match flags
                            criteriaMatch1 = False
                            criteriaMatch2 = False

                            'Check against criteria for the first cell of the pair
                            For xx = 1 To 5
                                If pairValue1 >= criteria(xx) - 10 And pairValue1 <= criteria(xx) + 10 Then
                                    criteriaMatch1 = True
                                    Exit For
                                End If
                            Next xx

                            'Check against criteria for the second cell of the pair
                            For yy = 1 To 5
                                If pairValue2 >= criteria(yy) - 10 And pairValue2 <= criteria(yy) + 10 Then
                                    criteriaMatch2 = True
                                    Exit For
                                End If
                            Next yy

                            'Check if criteria values are opposite of each other
                            If criteriaMatch1 And Not criteriaMatch2 Then
                                For zz = 1 To 5
                                    If pairValue2 = criteria(zz) + 10 Then
                                        criteriaMatch2 = True
                                        Exit For
                                    End If
                                Next zz
                            ElseIf criteriaMatch2 And Not criteriaMatch1 Then
                                For zz = 1 To 5
                                    If pairValue1 = criteria(zz) - 10 Then
                                        criteriaMatch1 = True
                                        Exit For
                                    End If
                                Next zz
                            End If

                            'If both criteria are met, format the cells
                            If criteriaMatch1 And criteriaMatch2 Then
                                'Set the border color and thickness for the pair of cells, using all round borders
                                With rng.Cells(j, k).Borders(xlEdgeLeft)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, k).Borders(xlEdgeRight)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, l).Borders(xlEdgeLeft)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, l).Borders(xlEdgeRight)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, k).Borders(xlEdgeTop)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, k).Borders(xlEdgeBottom)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, l).Borders(xlEdgeTop)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                                With rng.Cells(j, l).Borders(xlEdgeBottom)
                                    .LineStyle = xlContinuous
                                    .color = color
                                    .Weight = xlThick
                                End With

                            End If
                        End If
                    Next l
                Next k
            Next m
        Next j
    Next i
End Sub
 
Last edited by a moderator:
Try as I might, I can't see exactly where you get the pairs from that give you those results. Best I can say at this point is that wherever that is, I might branch off by calling a function that looks something like
VBA Code:
Private Function CalculatePairs(pair1 As Long, pair2 As Long) As Boolean
Select Case Abs(pair1-pair2)
    Case 1,2,3,5,7
         CalculatePairs = True
    Case Else
         CalculatePairs = False
End Select
The call might come after 'Check if criteria values are opposite of each other
and might look like
CalculatePairs pairValue1, pairValue2
or
If CalculatePairs pairValue1, pairValue2 = True Then 'do something if True, something else if False
EDIT - Note that ABS function converts negative values to positive, so since you are concerned with both, converting -1 to 1 is basically the same situation as converting +1 to +1 (which it doesn't - I'm just trying to explain the outcome of using ABS)
HTH
 
Upvote 0
Try as I might, I can't see exactly where you get the pairs from that give you those results. Best I can say at this point is that wherever that is, I might branch off by calling a function that looks something like
VBA Code:
Private Function CalculatePairs(pair1 As Long, pair2 As Long) As Boolean
Select Case Abs(pair1-pair2)
    Case 1,2,3,5,7
         CalculatePairs = True
    Case Else
         CalculatePairs = False
End Select
The call might come after 'Check if criteria values are opposite of each other
and might look like
CalculatePairs pairValue1, pairValue2
or
If CalculatePairs pairValue1, pairValue2 = True Then 'do something if True, something else if False

HTH
Please check this and help edit the code


SUNDAY SPECIAL.xlsm
ABCDEFGHIJKLMNOPQRS
16056533975116113991351099513192128114
2
336330895834246537486582612375
41431724546112245053456821327
5221785234672441223436451219
6298760262023203712341316178744
724383245069257230677410573389
85251231538211496770471251611
950418375228027661750741324434
1084108913194176551336387195749
11179701288893514763835556318
12176953902237531835606581446812
1356351493320587562314220582430
1463372211146939627693488131783
15308662375214333473494273333163
1627401169762546372794303183
1781196016530274783182945593227
182827153337395114497447148
195918681691130720253734416743
201879696116547757176363981588
216065780508147328428766463026
2289205553606568571291578552088
23612555471625231862632723675035
244988854474571058291533713694
256528650575323628569061378920
26631945166186335276253887864
2756823411498742359293748714245
282716148793701746127585691543
2924913232837646142803087784489
3024236121441858193656862294847
31367927111958792114208027534044
328874458523260595564361767567
33493155688662598689756832227960
34828385373335752131784639638
358939379778363013457083738566
365453181573069149386671376558
3724606733430832036704919677511
3824268283435084755837628298884
393121177775535135428774245857
4016706804055335241426916498563
419011632119658792329347552870
4282234721632354411381413201217
4321731322975263038502361125415
444873523970209871936593648365
4550761510341852134163034535255
466660407334789126340877531347
4731390772125871230752690284761
4875561358287637581853873075762
493145827356723929375544335130
5015235821636493716682270483837
517819885821335186647771343169
5287565848664944132123826394368
535523734128056355316278647785
54773380566382742738605934545721
555172343421580686248768326387
5673417182738567072595214536744
5750532104238891341834226395411
58691037142104840865541613274
59842958618190656225831756326865
6083280384781347169588977468866
6171836258243162869737557283111
628421248896558501532151655780
63819031418975515017183017385
64727922271130773575462712754523
657779788115115925169491446376
6660566287838245226478477126918
67782533376276057356817540442
685543395241165528254829736840
692128617527498274603487025925
70223825178187412274562936683738
71487918382529643543365387388839
723147593453812021765825308885
73896468167814862278321620558165
744832491038173533132148880234
7553845372417331352154155403332
7615756639283332355054156186688
7786637428691635339141254434145
SUN SPEC
Cell Formulas
RangeFormula
H1H1=SUM(B1,C1)
I1I1=SUM(B1,D1)
J1J1=SUM(B1,E1)
K1K1=SUM(B1,F1)
L1L1=SUM(C1,D1)
N1N1=SUM(C1,E1)
O1O1=SUM(C1,F1)
P1P1=SUM(D1,E1)
Q1Q1=SUM(D1,F1)
R1R1=SUM(E1,F1)
Named Ranges
NameRefers ToCells
'SUN SPEC'!Print_Area='SUN SPEC'!$C$1:$BC$106N1:O1, L1, H1
 
Upvote 0
Let me explain further. The code is to make Excel find all two cells in the same row that has sum total equal to any of the cells in H1 to R1. The cells must have value differences of +1,+2,+3,+4, and +7 and then -1,-2,-3,-5 and -7 of any two cells in B1 to F1. The cells that makes a pair should have value differences opposite of each other. Example; Cells D15 and E15 have values of 62 and 37 respectively. Which are +2 and -2 of cells B1 and E1 whose values are 60-39 respectively. Cells D15 and E15 has sum value of 99 which equals the sum value of cell J1.

Now I want Excel to find only cells with the difference of : +1 and -1, +2 and -2, +3 and -3, +5 and -5 and the +7 and -7 of any two cells in B1 to F1 and sum up to any of the target cells in H1 to R1.
 
Upvote 0

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