+ Reply to Thread
Results 1 to 9 of 9

Sorting Excel sheet by VBA

  1. #1
    Registered User
    Join Date
    05-05-2006
    Posts
    22

    Sorting Excel sheet by VBA

    Hi everyone, can anyone help me please?

    I have an Excel spreadsheet. Rows 1 through 7 are used by column headers and some general information about the info that is displayed. Rows 8 and up contain the actual data.

    New data is entered into the worksheet from a form. The form, currently, appends the new row of data across the bottom of the existing data. I'd like the same button that does that to then resort the worksheet so that it's listed in alphabetical order according to column B. (Column A is blank)

    I have tried it with this code:

    Please Login or Register  to view this content.
    but that doesn't work. Can anyone help me out?

    Richard

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565
    Hi Richard,

    Let me know if the following does the trick:

    Please Login or Register  to view this content.
    HTH

    Robert

  3. #3
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Or without selecting try

    Please Login or Register  to view this content.
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Excel will sort the whole data including the data above if you use currentregion. Specify the Range to sort, I have set the last Column as D amend to fit your table.

    Please Login or Register  to view this content.
    Last edited by royUK; 10-19-2007 at 01:06 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    05-05-2006
    Posts
    22
    Thanks, everyone. I got it to work. The problem I was having turned out to be that the sheet "Products" isn't the active sheet while the app is running. putting in a line to make it so did the trick. Here's the actual code that's working as required.

    Please Login or Register  to view this content.
    Thanks for everyone's help.

    Richard

  6. #6
    Registered User
    Join Date
    05-05-2006
    Posts
    22

    "Tidier" code doesn't work.

    Hi, can someone explain why this:

    Please Login or Register  to view this content.
    works, but this:

    Please Login or Register  to view this content.
    doesn't?

    Surely I should be able to sort the range just by .Sort, but to make it work I have to .Select, then do Selection.Sort?

    Richard

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Richard,

    Try activating the worksheet before you sort...
    Please Login or Register  to view this content.
    Sincerely.
    Leith Ross

  8. #8
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    Just FYI, this can be done without activating the sheet. The problem is that the macro is trying to sort data in an inactive sheet by a cell in the active sheet. To solve this (without activating the sheet to sort) you could do something like this:
    Please Login or Register  to view this content.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    My code doesn't need to select the sheet, nor does it use any looping.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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