+ Reply to Thread
Results 1 to 7 of 7

How to turn this into private function w/ arguments

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    How to turn this into private function w/ arguments

    I've got a bit of code that works great, but I'd like to turn it into a private function w/ arguments so I can avoid hard coding info each time I use it in another piece of code. What I have that works great is:

    Set ws = Worksheets("Report")
    ws.Columns("H:J").Sort Key1:=ws.Range("J2"), Order1:=xlAscending, Header:=xlYes, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    Ideally, I'd like to pass, as arguments, the sheetname, columns, column to sort on, and ascending or descending. You can assume that the sheets will all be in the same workbook. Also, would like to do it w/o selecting or activating if that's possible.

    Thanks.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to turn this into private function w/ arguments

    Perhaps
    Sub SortRange(rangeToSort as Range, columnToSortBy as long, optional order as xlSortOrder = xlAscending)
    rangeToSort.Sort Key1:=rangeToSort.Cells(1, columnToSortBy), Order1:=order, Header:=xlYes, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    End Sub
    and call it thus
    SortRange ws.Columns("H:J"), 3
    Last edited by xlnitwit; 10-30-2017 at 12:16 PM. Reason: Missing bracket
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: How to turn this into private function w/ arguments

    When I run the code, using this to call:
    Set ws=Worksheets("Report")
    SortRange ws.Columns("H:J"), 3
    I get 'wrong number of arguments or invalid property assignment" and it errors/debugs on this line:
    Private Sub SortRange(rangeToSort As Range, columnToSortBy As Long, Optional order As XlSortOrder = xlAscending)

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to turn this into private function w/ arguments

    I had left a bracket out of the code, which I have corrected above. Please copy the updated code and try it again.

  5. #5
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: How to turn this into private function w/ arguments

    That's awesome! Works great now. One last question, what would the "call" statement look like if I wanted to change the sort order?

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to turn this into private function w/ arguments

    You just add the additional argument
    SortRange ws.Columns("H:J"), 3, xlDescending

  7. #7
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: How to turn this into private function w/ arguments

    Sweet! Thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Private vs not in sub function using Buttons
    By cajunlibra in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2016, 03:34 PM
  2. [SOLVED] Get a value from last used row using private function
    By pandemic in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2015, 10:04 AM
  3. [SOLVED] Passing Arguments to PRIVATE Procedures
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2014, 10:34 AM
  4. How to Keep a Function Private but Available at Different Levels?
    By Tirren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2008, 07:21 PM
  5. Private Sub Worksheet_Change:do i need to turn them into one?
    By alex1982 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2007, 06:43 PM
  6. [SOLVED] Need to open the Function Arguments window from VBA for a user defined function.
    By korrin.anderson@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2006, 10:55 AM
  7. Calling Private Sub/Function
    By AMK4 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2006, 09:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1