VBA Enum - how to pass Enum as parameter,in function.

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

How to use Enum in Function to reduce lenghty parameter in functions.
In my below code Can I use Enum for Optional parameter.?.. and how to use.

VBA Code:
Option Explicit
Sub Sort_Data()

    Dim datarng As Range
   
    With Sheets("Data")
        Set datarng = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    End With
       
    'Sorting through Custom Function  '('how to create and pass in Enum for Optional Parameter in Function)
    Sort_Data_Asc datarng, datarng.Range("A1"), xlAscending,xlyes
       
End Sub



Function Sort_Data_Asc(rng As Range, k As Range, Optional Order1 As XlSortOrder = xlAscending, Optional Header As XlYesNoGuess = xlYes)

    rng.Sort k, Order1, Header:=Header

End Function

Below is a Table

NameScores
Rohit
7000​
Sachin
6000​
Virat
9000​
Yuvraj
3000​
zeb
8000​
Jadeja
2500​


Regards,
mg
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Yes, you can do that
VBA Code:
Sort_Data_Asc datarng, datarng.Range("a1"), 2, 1

Function Sort_Data_Asc(rng As Range, k As Range, Optional Order1 As Long = 1, Optional Header As Long = 1)
 
Upvote 0
Hi Fluff,

Thanks for your reply,
that is one way and it worked, something I am looking for below its working ,but I am not sure whether this method is right.

VBA Code:
Public Enum mySorting
    MysortAsc = 1
    MysortDesc = 2
End Enum

Sub Sort_Data()

    Dim datarng As Range
    
    With Sheets("Data")
        Set datarng = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    End With
        
    'Sorting through Custom Function
    Sort_Data_Asc datarng, datarng.Range("A1"), 1
End Sub

Function Sort_Data_Asc(rng As Range, k As Range, mySorting, Optional header As XlYesNoGuess = xlYes)
Select Case mySorting
    Case MysortAsc
    rng.Sort k, 1
    Case MysortDesc
     rng.Sort k, 2
    
End Select

End Function
 
Upvote 0
Why not just use the numbers as I showed?
 
Upvote 0
Hi Fluff,

thank you for your help, Your approach is right,
I was just creating Custom function which we can directely use in project. by importing Utilities.
and also trying to learn Enum. thanks fluff

Sort_Data_Asc datarng, datarng.Range("A1"), 1


Regards,
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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