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
 
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
@eiloken Hello,

Thank you for the awesome code. I liked how you used the logic to find the criteria and Round to handle the decimal situations. I just have a few doubts to clear.

The output value, however, does not exactly match the criteria in the above mini-sheet. It is close to the criteria but I need an exact match. Am I missing something?

Also, please see the new mini-sheet watched in this reply. The code is not working in this case. Please, check the code for this data too.

Thank you again for your dedication. It is really impressive.

New data:
Amjadinsaudia_Solved (2).xlsm
AB
1Filter RangeCriteria
2139414874.68
31386.64
4532
526
6486
7220.66
81062
9400
10126.27
11170
123810
13700
14500
1522
16120
17230
1835.65
19421.74
202129.56
21241.5
22234.78
23200
24230
25570
261843.48
27358.27
2886.96
2915
3035
31125
3230
33900
3420
35150
3620
3770
3850
3940
40200
4115
42435
437617.39
4417.39
4586.96
46150
47100
48430
49215
5060
5160
52280
531100
54157
55400
56180
57300
58220
59522.91
6045
61100
62200
6340
6450
653300
66100
67250
68250
69150
70400
711400
7215
7365
741200
7580
76152.17
77150
78930
791505
80116
8160
8260
83530
8480
8550
86570
87300
88100
89100
9043.48
9134.78
9234.78
9320
94100
95200
962500
971500
9847.83
99130.44
100547.92
101321.77
1021300
10380
10450
10550
106324.35
10780
10850
10960
110290
111115
11217.39
11370
11460
1154000
116270
117100
11862.5
119520
12030
121100
122634.8
123278.26
124140
125289.57
12668
1271400
1281000
129180
13030
13180
132117
13314.95
13470
Sheet1


Hints: Here, the expected output rows will be Row no. 7-15, 20-23, 56-66. Hope it helps.

Best Regards.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The output value, however, does not exactly match the criteria in the above mini-sheet. It is close to the criteria but I need an exact match. Am I missing something?

Also, please see the new mini-sheet watched in this reply. The code is not working in this case. Please, check the code for this data too.
Hello @eiloken ,

I apologize if tagging you like this is inconvenient for you. I was wondering if you need any additional information regarding this. And please, specify if this is not doable for you at this moment. Then, I could move on and think of something else for this matter.

Thank you.
 
Upvote 0
Hello @eiloken ,

I apologize if tagging you like this is inconvenient for you. I was wondering if you need any additional information regarding this. And please, specify if this is not doable for you at this moment. Then, I could move on and think of something else for this matter.

Thank you.
Sorry, i missed your feedback. yeah, my code really had a problem, it loop for each cell from top of range so it will ignore random cell in range that has sum equals to criteria, but in large data like your, i think to find all match cells, the code will loop for factorial of cells number so may be it so hard to do it in vba
 
Upvote 0
Sorry, i missed your feedback. yeah, my code really had a problem, it loop for each cell from top of range so it will ignore random cell in range that has sum equals to criteria, but in large data like your, i think to find all match cells, the code will loop for factorial of cells number so may be it so hard to do it in vba
Yes, I figured. Thank you for the specification. Best Regards.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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