Results 1 to 11 of 11

Active Workbook issue

Threaded View

  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?

     Range(Selection, Selection.End(xlDown)).Select
        ActiveWorkbook.Worksheets("Region 2").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Region 2").Sort.SortFields.Add Key:=Range( _
            "J3:J9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Region 2").Sort
            .SetRange Range("A3:M4783")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    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.

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