Macro to sort cells containing references

valle89

New Member
Joined
Jul 23, 2013
Messages
1
Hi! I'm relatively new to Excel and VBA and I could really need some help with the following problem. I'm truly sorry if I'm posting a question already asked, after hours of googling I haven't found an answer that would suit my situation (although I think this will be relatively easy for someone more experienced!)

The worksheet I use looks something like below. In column A I have different values and in column B an ID code to correspond every single of those values. E.g. values in column A could be price and values in column B a product code. All cells in columns A and B contain references to another worksheet (so basically my values and ID codes are fetched from another worksheet with Index and Match function combination and they are not static, i.e. if I change certain settings the values and codes in columns A and B change too).

My goal is to find the three biggest values from column A with corresponding ID from column B. In cells D3:D5 I've used Large function to find the three biggest values from column A and in cells E3:E5 Vlookup function to find the ID numbers matching those values. The problem is that Vlookup only returns the first occurrence, i.e. if I have duplicate values in column A, I get the same ID twice. So in cell E4 i have the ID 11 instead of 33.

To solve this I've tried to create a VBA macro that would automatically sort columns A and B in respect to values in column A so that I could easily link my top three value-ID combinations to columns D and E. So far I've only managed to construct macros that either don't work or crash my workbook altogether. It seems that having cells containing references somehow imposes problems. At the end of the day all I want is to return the three biggest values from column A with corresponding ID from column B so I'm happy with any kind of solution that does the trick, even if it didn't use the sorting approach I have tried.

I would really appreciate if someone could help me! Thank you for your time.

ABCDEFG
1ValueID
2611Three biggest valuesID
3122611
4633611
5244244
6#N/A#N/A
7#N/A#N/A

<tbody>
</tbody>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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