Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:13+00:00 2026-05-25T15:37:13+00:00

I have a problem with a sort function. It doesn’t work. If I record

  • 0

I have a problem with a sort function. It doesn’t work. If I record a macro while sorting manually, the recorded looks like mine. The Sub looks as follows:

Public Sub sortSelectionByDate(ByRef wrksheet As Worksheet, ByVal fromRow As Integer, ByVal toRow As Integer)
    'debug
    toRow = toRow - 1
    wrksheet.Select
    wrksheet.Rows(fromRow & ":" & toRow).Select
    With Selection
        .Sort Key1:=Range("A9"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub

Thank you in advance….

Christoph

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:37:13+00:00Added an answer on May 25, 2026 at 3:37 pm

    It’s most likely not working because you’re passing a worksheet that isn’t the active worksheet, not activating it, then trying to sort. Here’s a link to a description of the problem you’re having: Beginning VBA: Select and Activate. Long story short, never ever use .Select or .Selection unless you want your user to be able to run code on the selection of his choice. Even then, still not always a great idea. Here’s your sub re-written in one line:

    Public Sub sortSelectionByDate(ByRef wrksheet As Worksheet, ByVal fromRow As Integer, ByVal toRow As Integer)
        wrksheet.Rows(fromRow & ":" & toRow - 1).Sort Key1:=wrkSheet.Range("A9"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Sub
    

    The important changes are:

    • No need to use .Select. If you did want to use it you’d need to use wrkSheet.Activate first.
    • Change Key1:=Range(“A9”) to Key1:=wrkSheet.Range(“A9”). Always helps to be explicit when working with cells/ranges.
    • The With statement isn’t needed since you’re only running one method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with how the List Sort method deals with sorting. Given
can anyone help, i have problem doing a sort, I thought i had it
i have one problem when i sort the NSMutablearray using date and time wise.
I have sort of a tricky problem I'm attempting to solve. First of all,
I have some sort of a design problem with my Django AJAX application. I
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
Let's say I have a function like this (it's only an example, so do
I have this function to sort a JavaScript array of objects based on a
I have been researching this problem and while there's lots of posts on various
I have problem in some JavaScript that I am writing where the Switch statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.