Consulting

Results 1 to 9 of 9

Thread: VBA sorting error

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location

    VBA sorting error

    Hi all,

    Why does this not work unless im actually on the sheet? This is part of a Userform and as long as i have the 'Runners' sheet open it works fine, but if i have another, it throws an error

    Sheets("Runners").Range("A:AA").Sort key1:=Range("D1"), order1:=xlAscending, Header:=xlYes

    Cheers

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    With Worksheets("Runners")
        .Range("A:A").Sort Key1:=.Range("D1"), Order1:=xlAscending  Header:=xlYes
    End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location
    Quote Originally Posted by xld View Post
    With Worksheets("Runners")
        .Range("A:A").Sort Key1:=.Range("D1"), Order1:=xlAscending  Header:=xlYes
    End With
    Doesnt seem to work?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What exactly does '... doesn't seem' mean?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think there was typo in XLD's answer.

    It should be .Range("A:AA")


    With Worksheets("Runners") .Range("A:AA").Sort Key1:=.Range("D1"), Order1:=xlAscending Header:=xlYes End With
    The important thing is the <dot>Range ("D1") within the With/End With block since that makes Runners the parent object in all cases, otherwise D1 is on whatever the active sheet happens to be

    Otherwise a small sample WB would help
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location
    Right,

    This works, but only if the correct sheet is selected.
    Sheets("Runners").Range("A:AC").sort Key1:=Range("D1"), _
    Order1:=xlAscending, Header:=xlYes
    I have a userform inputting data into a form on a separate sheet. The user form has a drop-down list that looks at the sheet, so i would like it to reorder once a new entry is put in and then the drop-down list will be in order

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Yes, but did you try the code in #5?

    The important thing is the <dot>Range ("D1") within the With/End With block since that makes Runners the parent object in all cases, otherwise D1 is on whatever the active sheet happens to be
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Yes, but did you try the code in #5?
    Yep, tried exactly what was written.
    I'll try and put it in a sample WB and share it. Knowing my luck it will work on that.

  9. #9
    VBAX Regular
    Joined
    Nov 2018
    Posts
    17
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Yes, but did you try the code in #5?

    Yes, it does work with the little example i just put together. Just had to add the , between the statements.

    Ill try it in my project later

    With Worksheets("Runners")
        .Range("A:AA").sort Key1:=.Range("D1"), Order1:=xlAscending, Header:=xlYes

Posting Permissions

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