+ Reply to Thread
Results 1 to 4 of 4

Problems with sorting a dynamicly resizing range

  1. #1
    Registered User
    Join Date
    07-22-2012
    Location
    The Shire
    MS-Off Ver
    Office 2013
    Posts
    51

    Problems with sorting a dynamicly resizing range

    I am having great problems with a VBA script in Excel.

    The point of the script is to take a range of cells and randomly spread them over five columns (the script only ever spreads the range over a single column at a time). I have decided to achieve that by choosing a random cell, cutting it to the new destination, sorting the range and redefining it by referencing the first cell and using "End xlDown" to avoid huge potential loops over empty cells.

    However I had problems with the "Sort" method. According to MSDN it should be able to be used with no arguments, causing it to sort defaultly, however I was unable to get it running with no arguments, so I gave it explicit cell references to sort by, everything sort of worked - sometimes it would work, other times it would throw a "The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank." error. And, of course, the cell being sorted by is within the data being sorted and it does work, when it is empty and I manually sort by it.

    In addition I started receiving the "Object required" error.

    This is the code that causes the problems:

    Please Login or Register  to view this content.
    "Upr_per_Day" is a defined integer with a value assigned. The cycle runs fine the first time, but then "Copy_Range.Cells(Random, 1).Cut (exe.Offset(i - 1, 0))" fails the second time with the aforementioned error.

    EDIT:
    After the helpful tip from Norie, I discovered the root of the problem of the "Object required" error as being the "Offset(i - 1, 0)" property. It, for some reason, causes the "exe" range to be undefined after the first iteration of the loop (which runs fine).
    After some fiddling, I have come to the conclusion that it just doesn't like the "Offset(0, 0)" expression. Setting it to "Offset(i - 1, 0)" or "Offset(0, 0)" causes it to work fine the first iteration, but the second it no longer exists.

    Therefore currently I have two problems:
    1. I do not know why the code does not like the expression "Offset(0, 0)" and would like to find out.
    2. The "Sort" method sometimes fails unexpectedly with the error "The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.

    Attached you can find the workbook, containing the macro.

    Book1.xlsm
    Last edited by Durarara; 12-17-2015 at 05:24 AM. Reason: Additional information added

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Problems with sorting a dynamicly resizing range

    Could you be deleting the range exe refers to?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-22-2012
    Location
    The Shire
    MS-Off Ver
    Office 2013
    Posts
    51

    Re: Problems with sorting a dynamicly resizing range

    Huh... now that I look into it - "exe" is defined in the first loop, but then suddenly is "object required" the second time around, even though it is not present in the loop, itself.

    EDIT:
    Apparently changing

    "Offset(i - 1, 0)"
    to
    "Offset(i, 0)"

    fixes that problem, but why is beyond me. Especially since I needed the first time around to be Offset(0, 0). It's not that I cannot go around it, but it is peculiar, as hell.

    EDIT 2:
    After some fiddling, I have come to the conclusion it just doesn't like the "Offset(0, 0)" expression. Setting it to "Offset(i - 1, 0)" or "Offset(0, 0)" causes it to work fine the first iteration, but the second it no longer exists.
    Last edited by Durarara; 12-16-2015 at 10:00 AM.

  4. #4
    Registered User
    Join Date
    07-22-2012
    Location
    The Shire
    MS-Off Ver
    Office 2013
    Posts
    51

    Re: Problems with sorting a dynamicly resizing range

    I'd hate to "bump", but can noone help 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: 15
    Last Post: 06-24-2014, 09:27 AM
  2. [SOLVED] Problems with sorting andf Pivot Table problems
    By Saturn in forum Excel General
    Replies: 4
    Last Post: 10-07-2012, 07:47 AM
  3. Resizing cells in a selection without resizing entire sheet
    By Danielle via OfficeKB.com in forum Excel General
    Replies: 4
    Last Post: 08-11-2006, 05:10 PM
  4. [SOLVED] Problems with Command Buttons Randomly Resizing themselves
    By Blue Aardvark in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2006, 08:10 AM
  5. resizing a range
    By don in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2006, 11:10 PM
  6. resizing a range-What do I need to do to get the range to be resized as desired?
    By Tim Kredlo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2005, 04:05 PM
  7. [SOLVED] Chart Resizing Problems
    By Pedro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2005, 11:06 AM

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