VBA LARGE and ABS Formula

gt213

Board Regular
Joined
Jul 5, 2016
Messages
59
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?
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,183
Office Version
  1. 2016
Platform
  1. Windows
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
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,183
Office Version
  1. 2016
Platform
  1. Windows
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
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,183
Office Version
  1. 2016
Platform
  1. Windows
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
 

gt213

Board Regular
Joined
Jul 5, 2016
Messages
59
Thanks Footoo. The solution from your first post worked perfectly for me.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,477
Members
414,070
Latest member
DuncanLucas

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
Top