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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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!

[TABLE="width: 368"]
<tbody>[TR]
[TD]id[/TD]
[TD]company name[/TD]
[TD]Value[/TD]
[TD]Value date[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£1,002[/TD]
[TD="align: right"]31/05/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£2,004[/TD]
[TD="align: right"]30/06/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£4,008[/TD]
[TD="align: right"]31/07/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£8,016[/TD]
[TD="align: right"]31/08/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£16,032[/TD]
[TD="align: right"]30/09/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£32,064[/TD]
[TD="align: right"]31/10/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£64,128[/TD]
[TD="align: right"]30/11/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£128,256[/TD]
[TD="align: right"]31/12/2015[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£256,512[/TD]
[TD="align: right"]31/01/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£513,024[/TD]
[TD="align: right"]29/02/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£1,026,048[/TD]
[TD="align: right"]31/03/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£2,052,096[/TD]
[TD="align: right"]30/04/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£4,104,192[/TD]
[TD="align: right"]31/05/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£8,208,384[/TD]
[TD="align: right"]30/06/2016[/TD]
[/TR]
[TR]
[TD]bb23[/TD]
[TD]Auto Fiedelity sales[/TD]
[TD="align: right"]£16,416,768[/TD]
[TD="align: right"]31/07/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£3,001[/TD]
[TD="align: right"]31/03/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£3,601.20[/TD]
[TD="align: right"]30/04/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£4,321.44[/TD]
[TD="align: right"]31/05/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£5,185.73[/TD]
[TD="align: right"]30/06/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£6,222.87[/TD]
[TD="align: right"]31/07/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£7,467.45[/TD]
[TD="align: right"]31/08/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£8,960.94[/TD]
[TD="align: right"]30/09/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£10,753.13[/TD]
[TD="align: right"]31/10/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£12,903.75[/TD]
[TD="align: right"]30/11/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£15,484.50[/TD]
[TD="align: right"]31/12/2016[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£18,581.40[/TD]
[TD="align: right"]31/01/2017[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£22,297.68[/TD]
[TD="align: right"]28/02/2017[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£26,757.22[/TD]
[TD="align: right"]31/03/2017[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£32,108.66[/TD]
[TD="align: right"]30/04/2017[/TD]
[/TR]
[TR]
[TD]bb54[/TD]
[TD]Hertford auto trades[/TD]
[TD="align: right"]£38,530.39[/TD]
[TD="align: right"]31/05/2017[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,005.00[/TD]
[TD="align: right"]31/05/2017[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,105.25[/TD]
[TD="align: right"]£42,916.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,210.51[/TD]
[TD="align: right"]£42,947.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,321.04[/TD]
[TD="align: right"]£42,978.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,437.09[/TD]
[TD="align: right"]£43,008.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,558.94[/TD]
[TD="align: right"]£43,039.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,686.89[/TD]
[TD="align: right"]£43,069.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,821.24[/TD]
[TD="align: right"]£43,100.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£2,962.30[/TD]
[TD="align: right"]£43,131.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£3,110.41[/TD]
[TD="align: right"]£43,159.00[/TD]
[/TR]
[TR]
[TD]bb16[/TD]
[TD]smith automotives[/TD]
[TD="align: right"]£3,265.93[/TD]
[TD="align: right"]£43,190.00[/TD]
[/TR]
</tbody>[/TABLE]
 
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,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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