+ Reply to Thread
Results 1 to 13 of 13

Trouble sorting a sheet without using the sheet name

  1. #1
    Registered User
    Join Date
    08-10-2017
    Location
    Austin
    MS-Off Ver
    2010
    Posts
    27

    Trouble sorting a sheet without using the sheet name

    I'm beating my head against a wall over here.

    I recorded a macro that included sorting, but the sheet name is constantly changing, so when I tried to run the macro it kept saying "object out of range". I tried to replace the name of the sheet with the Excel codename "Sheet1" and now it's showing this error: "Run-time error '424': Object required."

    The first line here is where the error occurs:

    Please Login or Register  to view this content.
    What am I doing wrong?
    Last edited by micina; 08-24-2017 at 01:37 PM.

  2. #2
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: Trouble sorting a sheet without using the sheet name

    I did a sample record macro listed below:
    Please Login or Register  to view this content.
    You might want to verify that your worksheet name is spelled correctly. Copy/Paste from the tab might be best to ensure name is the same.

    Also, in lines 2-3 you are sorting on Range("K1") whereas later you are sorting on Range("A2:K6")

    Not sure if these will fix your issue, but might be a place to start looking...

  3. #3
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Trouble sorting a sheet without using the sheet name

    Try this:
    Please Login or Register  to view this content.
    Please click the Add Reputation star below any helpful posts, and use Thread Tools (up top) to mark your thread as SOLVED once you have your answer. Thanks!-Lee
    Last edited by leelnich; 08-10-2017 at 11:00 AM.

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Trouble sorting a sheet without using the sheet name

    Or just:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  5. #5
    Registered User
    Join Date
    07-19-2017
    Location
    Holland
    MS-Off Ver
    2013
    Posts
    10

    Re: Trouble sorting a sheet without using the sheet name

    I'm not sure but if I understand your question correct the name of the worksheet you need to work with is variable. You can solve this with the following code if I'm correct (I'm no expert either):

    first declare a variable for the worksheet name:

    Dim shtName As String

    shtName = ActiveSheet.Name

    Now replace "Sheet1" for shtName (don't use " " since you are referencing to a variable and not a static String value)

    I hope it will work

  6. #6
    Registered User
    Join Date
    08-10-2017
    Location
    Austin
    MS-Off Ver
    2010
    Posts
    27

    Re: Trouble sorting a sheet without using the sheet name

    I replaced the section of code I had originally had with what you wrote, leelnich, and while there was no error that came up, it didn't sort properly. (I'm sorting a column of dates, if that helps.)

  7. #7
    Registered User
    Join Date
    08-10-2017
    Location
    Austin
    MS-Off Ver
    2010
    Posts
    27

    Re: Trouble sorting a sheet without using the sheet name

    Tried to use JordtenBulte's suggestion (but changed the suggested name to something more specific to the macro's purpose) and the "Object required" error came up again. Here's what my code looks like now:

    Please Login or Register  to view this content.
    Last edited by micina; 08-24-2017 at 01:38 PM.

  8. #8
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Trouble sorting a sheet without using the sheet name

    OK please confirm:
    1) Your intention is to sort the ActiveSheet, that is, the VISIBLE currently-selected sheet in the currently-selected workbook.
    2) You intend to sort on column K
    3) Column K contains TEXT dates, not NUMERIC dates. (the cell Number Format is set to Text, not Date or Custom)

    BTW, forum rules require you to enclose code within code tags. Just select the relevant text and click the # icon on the Edit Ribbon. Thanks!
    Last edited by leelnich; 08-10-2017 at 12:04 PM.

  9. #9
    Registered User
    Join Date
    08-10-2017
    Location
    Austin
    MS-Off Ver
    2010
    Posts
    27

    Re: Trouble sorting a sheet without using the sheet name

    @leelnich

    1. Yes
    2. Yes
    3. Hmm this is probably the issue. Column K is a list of dates that are listed as dates, not as text. However, switching over to text stores it as a garbage list of numbers. In the code, could I switch the dates to text, then sort, then switch back to dates?

  10. #10
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Trouble sorting a sheet without using the sheet name

    Actually, you WANT numbers. I simply noticed you included DataOption:= xlSortTextAsNumbers in your sort parameters and assumed you had text. As a test, please enter this in a cell somewhere: =ISNUMBER(K5), where K5 is one of your dates. You should get TRUE if it's an Excel date.

    Now, all that being said, @Olly's post #4 uses the Range.Sort method. It's an older Sorting method, a bit less flexible, but perfectly adequate for your needs. And since you're sorting the active sheet, you can skip sheet references altogether! Try this:
    Please Login or Register  to view this content.
    Please click the Add Reputation star below any helpful posts, and use Thread Tools (up top) to mark your thread as SOLVED once you have your answer. Thanks!-Lee
    Last edited by leelnich; 08-10-2017 at 03:18 PM.

  11. #11
    Registered User
    Join Date
    08-10-2017
    Location
    Austin
    MS-Off Ver
    2010
    Posts
    27

    Re: Trouble sorting a sheet without using the sheet name

    Ah!! This worked!! Thank you so much, both of you!

  12. #12
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Trouble sorting a sheet without using the sheet name

    You're most welcome! If complete, please mark your thread as SOLVED (Thread Tools up top). Thanks -Lee

    Please click the Add Reputation star below any helpful posts, and use Thread Tools (up top) to mark your thread as SOLVED once you have your answer. Thanks!-Lee

  13. #13
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Trouble sorting a sheet without using the sheet name

    Hey Guys

    Posts One and Seven do not have Code Tags.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

+ 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. Replies: 16
    Last Post: 04-20-2017, 12:18 PM
  2. Replies: 5
    Last Post: 12-30-2014, 12:41 PM
  3. Replies: 12
    Last Post: 01-28-2014, 12:36 PM
  4. Replies: 0
    Last Post: 08-08-2012, 10:20 AM
  5. Replies: 2
    Last Post: 04-14-2012, 08:19 AM
  6. Having trouble importing a sheet of data from a closed workbook into a New Sheet.
    By Mr Cham in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-26-2010, 04:31 AM
  7. Replies: 2
    Last Post: 02-25-2009, 02:09 AM

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