+ Reply to Thread
Results 1 to 9 of 9

Sorting certain ranges in a worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    05-11-2017
    Location
    Belgium
    MS-Off Ver
    2010
    Posts
    22

    Sorting certain ranges in a worksheet

    Hi all,

    I use the following code for sorting named ranges by hour. But it sorts all the ranges in the worksheet and I only want the ranges that start with 'mission'.

    Can the
    For Each NamedRng In ActiveWorkbook.Names
    be changed so that it only sorts the ranges in the workbook that start with mission?



    'Startrij voor de lijst instellen = Rij 1 fungeert als "hoofding"
    R = 2
    'Ranges naar lijst kopiëren - Opdracht en uur
    For Each NamedRng In ActiveWorkbook.Names
        LijstWks.Cells(R, 1) = NamedRng.Name
        LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
        R = R + 1
    Next NamedRng
        
    'Ranges sorteren in de lijst
    R = R - 1
    Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)
    ListRng.Sort Key1:=ListRng.Cells(1, 2), Order1:=xlAscending
           
    'Ranges kopiëren naar SortWks
    R = 1
    For I = 1 To ListRng.Rows.Count
        Set Rng = ActiveWorkbook.Names(ListRng.Cells(I, 1).Text).RefersToRange
        Rng.Cut Destination:=SortWks.Cells(R, 1)
        R = R + Rng.Rows.Count
        
    Next I
    
    
    'Opdrachten naar detail kopiëren
    R = 1
    Worksheets("SortWks").Range("A1:U493").Cut Destination:=Worksheets("detail").Range("A5:U497")

  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: Sorting certain ranges in a worksheet

    Perhaps this
    For Each NamedRng In ActiveWorkbook.Names
       If Left$(lCase$(NamedRng.Name, 7)) = "mission" then
          LijstWks.Cells(R, 1) = NamedRng.Name
          LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
          R = R + 1
       end if
    Next NamedRng
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    05-11-2017
    Location
    Belgium
    MS-Off Ver
    2010
    Posts
    22

    Re: Sorting certain ranges in a worksheet

    That seems to do the job but gives an error on the following line below.


    Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)

    Any idea why?

  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: Sorting certain ranges in a worksheet

    You didn't delete the R = 2 line at the start by any chance, did you?

  5. #5
    Registered User
    Join Date
    05-11-2017
    Location
    Belgium
    MS-Off Ver
    2010
    Posts
    22

    Re: Sorting certain ranges in a worksheet

    No, it's still there...

  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: Sorting certain ranges in a worksheet

    What is the error message?

  7. #7
    Registered User
    Join Date
    05-11-2017
    Location
    Belgium
    MS-Off Ver
    2010
    Posts
    22

    Re: Sorting certain ranges in a worksheet

    I had to translate it but this should be the error message:

    Error 1004 "Application-defined or Object-defined error"

  8. #8
    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: Sorting certain ranges in a worksheet

    Then I think that none of your named ranges start with "mission". If that were the case, you'd start with R = 2 and never increment it. Then there is an R = R - 1 line (so R = 1) and finally you try to resize a range to be R - 1 rows and of course you cannot have a 0 row range.

  9. #9
    Registered User
    Join Date
    05-11-2017
    Location
    Belgium
    MS-Off Ver
    2010
    Posts
    22

    Re: Sorting certain ranges in a worksheet

    Tried it again a couple of times and eventually it worked. I left the 'LCase' out. I probably made a mistake somewhere, but it's perfect now!

    Thanks!

+ 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. Help w VBA Sorting Named Ranges
    By jlheath01 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2014, 10:51 AM
  2. Sorting 2 data ranges by comparing one column in each and sorting to match
    By MDKsmiffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-17-2013, 03:30 PM
  3. Replies: 0
    Last Post: 04-08-2013, 12:14 PM
  4. Sorting Value Ranges
    By inspiresit in forum Excel General
    Replies: 5
    Last Post: 06-15-2010, 05:54 PM
  5. Sorting ranges
    By timbim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2009, 10:59 AM
  6. Sorting Non Contiguous Ranges
    By tekman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-24-2009, 04:58 PM
  7. sorting non contiguous ranges
    By gsh20 in forum Excel General
    Replies: 1
    Last Post: 09-08-2005, 12:05 PM

Tags for this Thread

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