Excel Formula to Lookup Values Based on Criteria

Teal

New Member
Joined
Oct 4, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have found your site through the Access forum. Here is the link. Where I have posted the same problem. I need to lookup data from a list for instance: A2:A10 based on the value of B2. The value of B2 must be the sum total of the filtered values. In other words, the sum of the lookup data must be equal to the criteria in B2. Can you help me find an Excel formula for this?

Regards,
Teal

Filter formula.png
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
try this, this code will loop through Filter Range and change all the cells color to yellow when sum of them match with critera:
VBA Code:
Sub SumWithMatchCrit()
    Dim cll As Range
    Dim test As Range
    Dim trng As Range
    Dim rng As Range
    Dim crit As Integer
    Dim tVal As Integer
    Set rng = Range(Cells(2, 1), Cells(10, 1))
    rng.Interior.Pattern = xlNone
    crit = Cells(2, 2).Value
    For Each cll In rng
        Set trng = cll
        For Each test In rng
            If Intersect(test, trng) Is Nothing Then
                tVal = test.Value + WorksheetFunction.Sum(trng)
                If tVal = crit Then
                    Union(trng, test).Interior.Color = RGB(255, 255, 0)
                ElseIf tVal < crit Then
                    Set trng = Union(trng, test)
                End If
            End If
        Next test
    Next cll
End Sub
 
Last edited:
Upvote 0
try this, this code will loop through Filter Range and change all the cells color to yellow when sum of them match with critera:
VBA Code:
Sub SumWithMatchCrit()
    Dim cll As Range
    Dim test As Range
    Dim trng As Range
    Dim rng As Range
    Dim crit As Integer
    Dim tVal As Integer
    Set rng = Range(Cells(2, 1), Cells(10, 1))
    rng.Interior.Pattern = xlNone
    crit = Cells(2, 2).Value
    For Each cll In rng
        Set trng = cll
        For Each test In rng
            If Intersect(test, trng) Is Nothing Then
                tVal = test.Value + WorksheetFunction.Sum(trng)
                If tVal = crit Then
                    Union(trng, test).Interior.Color = RGB(255, 255, 0)
                ElseIf tVal < crit Then
                    Set trng = Union(trng, test)
                End If
            End If
        Next test
    Next cll
End Sub
remember this will loop to find all values match with your condition so if in your range have multiple group of cells that have sum math with your criterial, all the cells will mark same as yellow color. In your example, i ran my code and it only have 3 cells match:
 

Attachments

  • Capture.JPG
    Capture.JPG
    13.8 KB · Views: 5
Upvote 0
How about this?

VBA Code:
Sub FindCombinations()
    Dim Numbers() As Variant
    Dim TargetSum As Long
    Dim Combinations As Collection
    Dim Combination As Variant
    Dim i As Integer, j As Integer, k As Integer
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim CombinationDict As Object
  
    Set ws = ThisWorkbook.Sheets("Sheet1")
 
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    ReDim Numbers(2 To lastRow)  '

    For i = 2 To lastRow
        Numbers(i) = ws.Cells(i, 1).value
    Next i
  
    TargetSum = ws.Range("B2").value
    Set Combinations = New Collection
    Set CombinationDict = CreateObject("Scripting.Dictionary")
  
    ' Loop through all possible combinations
    For i = 2 To 2 ^ lastRow - 1
        Dim CurrentCombination As String
        CurrentCombination = ""
        Dim CurrentSum As Long
        CurrentSum = 0
      
        For j = 2 To lastRow
            If (i And (2 ^ (j - 1))) <> 0 Then
                CurrentSum = CurrentSum + Numbers(j)
                CurrentCombination = CurrentCombination & Numbers(j) & ","
            End If
        Next j
     
        If CurrentSum = TargetSum Then
     
            If Not CombinationDict.Exists(CurrentCombination) Then
                Combinations.Add Left(CurrentCombination, Len(CurrentCombination) - 1) ' Remove the trailing comma
                ' Add the combination to the dictionary to mark it as seen
                CombinationDict(CurrentCombination) = 1
            End If
        End If
    Next i
  
    ' Display the valid combinations
    For Each Combination In Combinations
        Debug.Print Combination
    Next Combination
End Sub
 
Upvote 0
try this, this code will loop through Filter Range and change all the cells color to yellow when sum of them match with critera:
VBA Code:
Sub SumWithMatchCrit()
    Dim cll As Range
    Dim test As Range
    Dim trng As Range
    Dim rng As Range
    Dim crit As Integer
    Dim tVal As Integer
    Set rng = Range(Cells(2, 1), Cells(10, 1))
    rng.Interior.Pattern = xlNone
    crit = Cells(2, 2).Value
    For Each cll In rng
        Set trng = cll
        For Each test In rng
            If Intersect(test, trng) Is Nothing Then
                tVal = test.Value + WorksheetFunction.Sum(trng)
                If tVal = crit Then
                    Union(trng, test).Interior.Color = RGB(255, 255, 0)
                ElseIf tVal < crit Then
                    Set trng = Union(trng, test)
                End If
            End If
        Next test
    Next cll
End Sub
Yes, it worked perfectly in this case. Thank you so much!
I have one more question. What if my data has both integer and decimal values? I have changed the crit and tVal as Double. But it is not working. Do I have to modify anything else?
 
Upvote 0
Yes, it worked perfectly in this case. Thank you so much!
I have one more question. What if my data has both integer and decimal values? I have changed the crit and tVal as Double. But it is not working. Do I have to modify anything else?
give me your data, i change tVal and crit to double and it still work:
Capture.JPG
 
Upvote 0
Yes, it worked perfectly in this case. Thank you so much!
I have one more question. What if my data has both integer and decimal values? I have changed the crit and tVal as Double. But it is not working. Do I have to modify anything else?
 
Upvote 0
give me your data, i change tVal and crit to double and it still work:View attachment 99774
Mydata.xlsm
AB
1Filter RangeCriteria
20.5163594.7
30.01
4932.2
5326.03
61020.34
711584.82
8341.74
9559.08
10217.36
1114915.06
128182.04
13233.05
144557.11
151521.45
16108.68
174069.32
1810017.45
19667.77
205125.87
212501.25
223419.65
231025.18
24574.78
25559.09
2612799.52
27326.03
282777.25
2912811.3
301259.25
317779.97
3214505.77
334128.22
3427144.71
357866
369257.92
373320.63
38450.41
391956.16
405554.5
4114025.1
422221.8
4315558.69
444238.36
45119.54
4610449.75
47543.38
481165.24
492777.25
5014834.16
5152442.01
5210413.25
532.97
5410042.4
5533791.91
56108.68
57108.68
58108.68
595739.26
60233.05
6119683.49
62217.35
632282.18
645554.53
656461.36
66108.68
6712123.31
6817919.39
695163.5
702608.2
71217.35
724020.98
735005.98
743701.3
7515032.11
765425.13
7732890.07
785071.5
79217.35
804347.04
819039
82108.68
83108.68
8413584.39
85108.68
863042.92
876749.97
8811004.67
89108.68
90108.68
91523.6
925808.08
9316255.25
945110.17
95760.73
961983.75
9763150.72
982448.38
991992.38
1003174
10122495.71
102326.03
103760.73
1045593.14
1056292.3
1062966.84
107217.36
1087670.51
10919167.4
1109357.28
1112646.18
1121412.78
113108.68
114434.7
1151165.24
1165977.15
1174570.71
118217.35
11919722.17
1205226.75
1211328.26
1227619.36
12333315.99
12414201.71
1251964.72
12610994.32
1275735.64
12813050.66
1296725.8
1303610.73
1318538.25
1321455.79
1333260.28
13422903.56
13532480.94
1369355.25
137543.38
1387084.42
139326.03
1405313.01
1416525.33
1429442.65
14317635.53
144396.75
1454890.39
1461398.29
1474464.43
148326.04
149793.5
1505977.14
15110254.09
15212280.3
153869.41
1546520.52
15520275.13
1562415
15723141.79
15814761.77
1593875.5
16010777.7
1618633.64
1623224.8
1631329.4
1648038.63
165664.13
1666878.17
167108.68
168326.03
169108.68
170543.38
1718351.29
1721190.25
17315214.52
17425571.69
175233.05
176108.68
17735069.59
17811511.5
17947028.42
1803523.33
1811110.9
1821412.78
1835358.91
1843471.57
185108.68
1862536.34
18718576.61
18825208.03
18916894.46
19022118.6
191683.46
1922825.56
193108.68
1949784.97
1953541.46
1968271.4
19729110.57
19868596.1
19911669.42
20015015.27
2016365.25
2024274.57
2034815.52
20421364.62
2055433.75
2063924.97
2075899.87
20815462.62
2091630.14
21022063.47
2115353.44
212396.75
2132415
21411664.46
2155808.08
216108.68
2176059.24
2184648.9
2195649.38
22017068.11
2215807.5
2229201.26
223217.35
2245925.96
22512294.35
2265119.81
227108.68
22818541.78
2297305.39
23010388.17
2312990
Sheet1
 
Upvote 0
How about this?

VBA Code:
Sub FindCombinations()
    Dim Numbers() As Variant
    Dim TargetSum As Long
    Dim Combinations As Collection
    Dim Combination As Variant
    Dim i As Integer, j As Integer, k As Integer
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim CombinationDict As Object
 
    Set ws = ThisWorkbook.Sheets("Sheet1")
 
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    ReDim Numbers(2 To lastRow)  '

    For i = 2 To lastRow
        Numbers(i) = ws.Cells(i, 1).value
    Next i
 
    TargetSum = ws.Range("B2").value
    Set Combinations = New Collection
    Set CombinationDict = CreateObject("Scripting.Dictionary")
 
    ' Loop through all possible combinations
    For i = 2 To 2 ^ lastRow - 1
        Dim CurrentCombination As String
        CurrentCombination = ""
        Dim CurrentSum As Long
        CurrentSum = 0
     
        For j = 2 To lastRow
            If (i And (2 ^ (j - 1))) <> 0 Then
                CurrentSum = CurrentSum + Numbers(j)
                CurrentCombination = CurrentCombination & Numbers(j) & ","
            End If
        Next j
    
        If CurrentSum = TargetSum Then
    
            If Not CombinationDict.Exists(CurrentCombination) Then
                Combinations.Add Left(CurrentCombination, Len(CurrentCombination) - 1) ' Remove the trailing comma
                ' Add the combination to the dictionary to mark it as seen
                CombinationDict(CurrentCombination) = 1
            End If
        End If
    Next i
 
    ' Display the valid combinations
    For Each Combination In Combinations
        Debug.Print Combination
    Next Combination
End Sub
Hello,

It is working for the example dataset perfectly too. But this code returns an overflow error for my large dataset (A2:A231). Any thoughts?
 
Upvote 0
Ok it happened because when vba calculate decimal number, it will compare exactly all the digits of decimal part so when you compare it with your criteria with round of decimal digits, it will not equals. So i think this will work:
VBA Code:
Sub SumWithMatchCrit()
    Dim lr As Long
    Dim cll As Range
    Dim test As Range
    Dim trng As Range
    Dim rng As Range
    Dim crit As Double
    Dim tVal As Double
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range("A2:A" & lr)
    rng.Interior.Pattern = xlNone
    crit = Cells(2, 2).Value
    For Each cll In rng
        Set trng = cll
        For Each test In rng
            If Intersect(test, trng) Is Nothing Then
                tVal = test.Value + WorksheetFunction.Sum(trng)
                If Round(tVal, 1) = Round(crit, 1) Then 'change the digit to what you need
                    Debug.Print Union(trng, test)
                    Union(trng, test).Interior.Color = RGB(255, 255, 0)
                ElseIf tVal < crit Then
                    Set trng = Union(trng, test)
                End If
            End If
        Next test
    Next cll
End Sub
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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