VBA LARGE and ABS Formula

gt213

Board Regular
Joined
Jul 5, 2016
Messages
61
How could i get the following excel formula to work using VBA:
{=LARGE(ABS($D$18:$D$23),G23)}

In this example i have my data in cells D18:D23 and in cells G18:G23 numbers 1 to 5, so with the formula above in H18 i can drag it down to H23 to get the 1st - 5th largest absolute values.

Using VBA how could i achieve the same results?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Here's one way :
Code:
Sub FT()
Dim dRng As Range: Set dRng = [D18:D23]
Dim rRng As Range: Set rRng = [H18:H23]
Dim nRng As Range: Set nRng = [G18:G23]
rRng(1).FormulaArray = "=LARGE(ABS(" & dRng.Address & ")," & nRng(1).Address(0, 0) & ")"
rRng(1).AutoFill Destination:=rRng, Type:=xlFillDefault
rRng = rRng.Value
End Sub
 
Upvote 0
And if you want to automatically update for any change to D18:D23, put this in the sheet module :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, [D18:D23]) Is Nothing Then Call FT
Application.EnableEvents = True
End Sub
 
Upvote 0
If you want to eliminate the numbers in G18:G23 :
Code:
Sub FT()
Dim dRng As Range: Set dRng = [D18:D23]
Dim rRng As Range: Set rRng = [H18:H23]
rRng.Formula = "=Abs(" & dRng(1).Address(0, 0) & ")"
rRng = rRng.Value
rRng.Sort Key1:=rRng(1), Order1:=xlDescending, Header:=xlNo
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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