+ Reply to Thread
Results 1 to 11 of 11

Active Workbook issue

  1. #1
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Active Workbook issue

    I am very new to writing/recording macros. I have code that works for sorting data in a worksheet. I recorded a macro, and then copied the code into my other macro in VBA. However, I want to be able to run the macro on any worksheet. When I recorded the macro, it identifies the specific active worksheet I was working in.

    How do I change the following code so that I can use this macro on any worksheet that I open?

    Please Login or Register  to view this content.
    I also want to change the range I sort to capture however many lines of data the new worksheet may have. It may have 4000 rows, it may have 6000 rows. How do I change that?

    Thank you!
    Last edited by annek; 09-29-2010 at 04:18 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Active Workbook issue

    Do you want to sort the entire used area of the worksheet?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    No. I want to sort the entire workbook by column J (starting at row2), and then where there are no values in column J, I want to sort by column H. I'm attaching a sample of my data--maybe that will help show what I need.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    Sorry--I said workbook, but I mean worksheet.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Active Workbook issue

    This sorts the active worksheet:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    That worked for sorting by column J. Thanks!

    Now, how do I sort all the data at the bottom of the worksheet (where there aren't values for J) by column H? At this point, I don't want to sort the entire worksheet. Just what doesn't have values for J.

  7. #7
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    I just looked at my data, and the code above sorts my column headings with the data, which I don't want. My column headings are on Row 3, so I changed "J2" to J3", but that didn't work. What can I do to fix this.

    Thanks for bearing with my VBA ignorance.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Active Workbook issue

    In your example, the header was in row 2; not so?

  9. #9
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    Correct, row 2 is my header. Your code works just like I want on my test file, however, when I run it on my larger worksheet (over 4700 lines), the header is sorted into my data, and the sort on column H doesn't appear to work.

  10. #10
    Registered User
    Join Date
    09-17-2010
    Location
    Allen, TX
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Active Workbook issue

    This is a more accurate example of what I'm working with, but on a much larger scale. When I run your macro on this, the header gets sorted along with the data.
    Attached Files Attached Files

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Active Workbook issue

    Please Login or Register  to view this content.

+ 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