Adding this VBA to a Function

Mahwill

New Member
Joined
Jun 8, 2011
Messages
6
Is there a way to add this VBA code into a function and only call the function instead of writing out this code every time?

Here is the code -
opt.Activate
If ActiveSheet.AutoFilterMode = True Then
opt.AutoFilter.Sort.SortFields.Clear
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
Else:
Range("A6:CK6").AutoFilter
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
With opt.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

The range that I sort on changes so I was wondering if I could declare that as a variable and just fill in the range I need when I call the function.

Any help would be greatly appreciated.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You could put the code in a Module and then simply call your code from anywhere in workbook/code.

In the Module create a Sub like Sub mycode(). Copy your code between the sub and end sub.

Once there you should be able to simply reference your code from other places in the code by simply typing Call mycode.
 
Upvote 0
As far as passing the range to your code you will likely need to create a parameter.

For example, Sub mycode(rng as Range)

When you call your code the syntax will be Call mycode(rng).

At some point before you call mycode you will need to declare and set the rng variable.
 
Upvote 0
Okay, here's what I have,
Sub sort(rng As Range)
Dim opt As Worksheet
Set opt = Sheets("OptimizerInfo")
If opt.FilterMode Then
opt.ShowAllData
End If
opt.Activate
If ActiveSheet.AutoFilterMode = True Then
opt.AutoFilter.sort.SortFields.Clear
opt.AutoFilter.sort.SortFields.Add Key _
:=Range(rng), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
Else:
Range("A6:CK6").AutoFilter
opt.AutoFilter.AutoFilter.sort.SortFields.Add Key _
:=Range(rng), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
With opt.AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

and I'm calling it using

Sub test()
Dim rng As Range
Set rng = Range("I6")
Call sort(rng)
End Sub

This isn't corrent though because it gives me the value within the cell not the range itself. How do I fix this?
 
Upvote 0
Change this...
Code:
opt.AutoFilter.sort.SortFields.Add Key _
:=[COLOR="Red"]Range(rng)[/COLOR], SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal

To this...
Code:
opt.AutoFilter.sort.SortFields.Add Key _
:=[COLOR="Red"]rng[/COLOR], SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal

Also, you should specify the sheet when you define rng in Sub test()
Code:
Sub test()
Dim rng As Range
Set rng = [COLOR="Red"]Sheets("OptimizerInfo").[/COLOR]Range("I6")
Call sort(rng)
End Sub
 
Last edited:
Upvote 0
Just an aside - don't call the sub sort.

Sort is a VBA method.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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