Pulling Latest value for each ID via vba

Smurphster16

New Member
Joined
Feb 28, 2019
Messages
25
Hi Guys,

Basically I have a fairly large spreadsheet which contains ID's and then values in the adjacent column with dates in the following column.

For each ID I want to take the value corresponding to the latest date, bearing in mind each ID could have a different number of values and therefore dates.

What is the best solution /is there a VBA solution to this?

Thanks in advance,

Phil
 

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.
It would be really helpful if you could post a sample of the data
 
Upvote 0
Hi,

You can have either an Array Formula, or a VBA solution ...

It depends on how you need your end result to be displayed ...
 
Upvote 0
If you sort the data on ID then DATE ascending, and assuming your three columns are in A,B and C, and if you also had the unique IDs in columns E started on E1, you could put this formula in F1 and drag it down

Code:
=INDEX(B:B,MATCH(E1,A:A,0)+COUNTIFS(A:A,E1)-1)
 
Upvote 0
Sounds like a formula solution is possible. Is your preference for a VBA solution?

Either way you can improve your chance of getting a solution if you post some sample data so we can see which columns/rows hold the data, at least for the ID,Value and Date data you describe in your post.
 
Upvote 0
Hi,

Could you describe your worksheet structure ...
 
Upvote 0
Hi guys this is a sample data for the problem;

I would like to know a vba and formula solution if possible please as this may help me further down the line.

Thanks so much!

idcompany nameValueValue date
bb23Auto Fiedelity sales£1,00231/05/2015
bb23Auto Fiedelity sales£2,00430/06/2015
bb23Auto Fiedelity sales£4,00831/07/2015
bb23Auto Fiedelity sales£8,01631/08/2015
bb23Auto Fiedelity sales£16,03230/09/2015
bb23Auto Fiedelity sales£32,06431/10/2015
bb23Auto Fiedelity sales£64,12830/11/2015
bb23Auto Fiedelity sales£128,25631/12/2015
bb23Auto Fiedelity sales£256,51231/01/2016
bb23Auto Fiedelity sales£513,02429/02/2016
bb23Auto Fiedelity sales£1,026,04831/03/2016
bb23Auto Fiedelity sales£2,052,09630/04/2016
bb23Auto Fiedelity sales£4,104,19231/05/2016
bb23Auto Fiedelity sales£8,208,38430/06/2016
bb23Auto Fiedelity sales£16,416,76831/07/2016
bb54Hertford auto trades£3,00131/03/2016
bb54Hertford auto trades£3,601.2030/04/2016
bb54Hertford auto trades£4,321.4431/05/2016
bb54Hertford auto trades£5,185.7330/06/2016
bb54Hertford auto trades£6,222.8731/07/2016
bb54Hertford auto trades£7,467.4531/08/2016
bb54Hertford auto trades£8,960.9430/09/2016
bb54Hertford auto trades£10,753.1331/10/2016
bb54Hertford auto trades£12,903.7530/11/2016
bb54Hertford auto trades£15,484.5031/12/2016
bb54Hertford auto trades£18,581.4031/01/2017
bb54Hertford auto trades£22,297.6828/02/2017
bb54Hertford auto trades£26,757.2231/03/2017
bb54Hertford auto trades£32,108.6630/04/2017
bb54Hertford auto trades£38,530.3931/05/2017
bb16smith automotives£2,005.0031/05/2017
bb16smith automotives£2,105.25£42,916.00
bb16smith automotives£2,210.51£42,947.00
bb16smith automotives£2,321.04£42,978.00
bb16smith automotives£2,437.09£43,008.00
bb16smith automotives£2,558.94£43,039.00
bb16smith automotives£2,686.89£43,069.00
bb16smith automotives£2,821.24£43,100.00
bb16smith automotives£2,962.30£43,131.00
bb16smith automotives£3,110.41£43,159.00
bb16smith automotives£3,265.93£43,190.00

<tbody>
</tbody>
 
Upvote 0
With your data in A1:D42, e.g. id bb23 ...

you could test following array formula

=INDEX(C2:C42,MATCH(MAX(IF(A2:A42="bb23",D2:D42)),D2:D42,0))

Hope this will help
 
Upvote 0
Copy the formulas in H2:I2 down.
Excel Workbook
ABCDEFGHI
1idcompany nameValueValue datesortidLatest DateValue
2bb23Auto Fiedelity sales2,0046/30/20150.02758bb163/31/20183,265.93
3bb16smith automotives2,558.9410/31/20170.02827bb237/31/201616,416,768
4bb23Auto Fiedelity sales256,5121/31/20160.06747bb545/31/201738,530.39
5bb16smith automotives2,321.048/31/20170.07741
6bb23Auto Fiedelity sales4,0087/31/20150.13401
7bb54Hertford auto trades7,467.458/31/20160.15235
8bb23Auto Fiedelity sales16,416,7687/31/20160.18378
9bb54Hertford auto trades5,185.736/30/20160.21211
10bb16smith automotives2,210.517/31/20170.23401
11bb54Hertford auto trades15,484.5012/31/20160.24165
12bb23Auto Fiedelity sales1,0025/31/20150.24625
13bb16smith automotives2,962.301/31/20180.27605
14bb54Hertford auto trades3,601.204/30/20160.2957
15bb23Auto Fiedelity sales8,0168/31/20150.34779
16bb54Hertford auto trades22,297.682/28/20170.35748
17bb23Auto Fiedelity sales4,104,1925/31/20160.35788
18bb16smith automotives2,105.256/30/20170.37374
19bb23Auto Fiedelity sales32,06410/31/20150.40699
20bb54Hertford auto trades4,321.445/31/20160.46551
21bb54Hertford auto trades26,757.223/31/20170.48363
22bb54Hertford auto trades32,108.664/30/20170.48545
23bb54Hertford auto trades18,581.401/31/20170.48678
24bb23Auto Fiedelity sales128,25612/31/20150.50437
25bb16smith automotives3,110.412/28/20180.53849
26bb16smith automotives2,005.005/31/20170.54067
27bb54Hertford auto trades3,0013/31/20160.58074
28bb54Hertford auto trades10,753.1310/31/20160.67167
29bb54Hertford auto trades8,960.949/30/20160.68949
30bb16smith automotives2,686.8911/30/20170.76434
31bb23Auto Fiedelity sales2,052,0964/30/20160.78184
32bb54Hertford auto trades6,222.877/31/20160.78331
33bb23Auto Fiedelity sales1,026,0483/31/20160.80445
34bb23Auto Fiedelity sales8,208,3846/30/20160.81514
35bb54Hertford auto trades12,903.7511/30/20160.87511
36bb16smith automotives3,265.933/31/20180.89053
37bb16smith automotives2,821.2412/31/20170.90305
38bb54Hertford auto trades38,530.395/31/20170.92425
39bb23Auto Fiedelity sales64,12811/30/20150.93088
40bb23Auto Fiedelity sales513,0242/29/20160.96551
41bb16smith automotives2,437.099/30/20170.98706
42bb23Auto Fiedelity sales16,0329/30/20150.99884
Sheet9
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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