+ Reply to Thread
Results 1 to 5 of 5

Active Selection turned into Range

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2017
    Location
    Colorado
    MS-Off Ver
    2017 and 2013
    Posts
    7

    Active Selection turned into Range

    Hello all,
    I have come across this problem multiple times and finally decided to receive help and solve this problem. In VBA I utilize Range(Selection, Selection.End(xlToLeft)).Select often in order to select a large range of data with a variable amount of rows or columns. This works well until I want to plug that active selection into a tool in Excel like Sort and Filtering.

    The below code and attached workbook is an example of this issue. This is a schedule spreadsheet that I would like to give to my superintendent. I would like him to be able to add rows to the schedule and then be able to press a button and sort it by beginning date but because the number of rows can change problems occur like filtering the wrong part of the sheet
    If there was a way to put the active selection into the range this would solve the problem or is there a way to write code to make VBA extract the active selection and output it as a range or something

    Thank you very much for any help,
    Andy
    'Sorting Code
             ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Add Key:=Range("AI10:AI38"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortNormal
        'I used record macro to figure out how to make vba sort but it wont let the range be the selected area
        'This will be a problem when the user adds or removes rows so I need vba to sort values in a range given
        ' that the number of rows could change
            With ActiveWorkbook.Worksheets("Schedule").Sort
                .SetRange Range("A10:AI38")
                .Header = xlGuess
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With

    Sub Sort()
    'This Sub sorts the values based on their date
    'Make sure the Sub runs on the correct sheet
        Sheets("Schedule").Select
        'Unhide the Match Equation Column
            Columns("AH:AJ").Select
            Selection.EntireColumn.Hidden = False
    
        'Seg 1 will always start on cell AI10
        Range("AI10").Select
        
    'Selects the entire range
        For Ctr = 1 To 23 'Set up to counteract the worst case of work EVERY OTHER DAY
            Range(Selection, Selection.End(xlToLeft)).Select
        Next
            Range(Selection, Selection.End(xlDown)).Select
        
    'Sorting Code
             ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Add Key:=Range("AI10:AI38"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortNormal
        'I used record macro to figure out how to make vba sort but it wont let the range be the selected area
        'This will be a problem when the user adds or removes rows so I need vba to sort values in a range given
        ' that the number of rows could change
            With ActiveWorkbook.Worksheets("Schedule").Sort
                .SetRange Range("A10:AI38")
                .Header = xlGuess
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
    
    '************************************************************************************
    '                                  Now to Seg 2
    '************************************************************************************
    
    'Find Seg 2 Civil
    'Initialize Column Counter
    Rctr = 10
        'Find Row
        Do Until Cells(Rctr, 1) = "SEG 2 CIVIL"
            Rctr = Rctr + 1
        Loop
    'Select the Ref Cell
        Cells(Rctr + 1, 35).Select
    
    'REPEAT CODE FROM SEG 1
        'Selects the entire range
            For Ctr = 1 To 23 'Set up to counteract the worst case of work EVERY OTHER DAY
                Range(Selection, Selection.End(xlToLeft)).Select
            Next
             Range(Selection, Selection.End(xlDown)).Select
            
        'Sorting Code
                ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Clear
                ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Add Key:=Selection, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                   xlSortNormal
                With ActiveWorkbook.Worksheets("Schedule").Sort
                    .SetRange Selection
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
    
    
    
    
    
    
    'After you run this code you will need to replace the X's with the
    'disordered ones if you would like to experiment more.
    'Copy and paste the second sheet to do this
    
    
    
    
    
    
        Columns("AI:AI").Select
        Selection.EntireColumn.Hidden = True
        Range("A1").Select
    End Sub
    Attached Images Attached Images
    Attached Files Attached Files

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

    Re: Active Selection turned into Range

    Perhaps you could use Selection.CurrentRegion which should return the 'region' the selected cell(s) are in.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-26-2017
    Location
    Colorado
    MS-Off Ver
    2017 and 2013
    Posts
    7

    Re: Active Selection turned into Range

    Selection.CurrentRegion still gives an error

    See attached Picture for more detail
    Attached Images Attached Images

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

    Re: Active Selection turned into Range

    How exactly did you try using Selection.CurrentRegion?

  5. #5
    Registered User
    Join Date
    06-26-2017
    Location
    Colorado
    MS-Off Ver
    2017 and 2013
    Posts
    7

    Re: Active Selection turned into Range

        'Sorting Code
                ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Clear
                ActiveWorkbook.Worksheets("Schedule").Sort.SortFields.Add Key:=Selection.CurrentRegion, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                   xlSortNormal
                With ActiveWorkbook.Worksheets("Schedule").Sort
                    .SetRange Selection.CurrentRegion
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With

+ 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. Selecting and clearing a Specific Range within an Active Selection
    By AustinR in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-02-2015, 01:19 PM
  2. Replies: 8
    Last Post: 08-06-2014, 04:41 AM
  3. [SOLVED] Use active cell address from one sheet as range selection for another sheet
    By JJGF in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2013, 10:26 PM
  4. Auto-populate a range based on active selection with data from another sheet
    By nomwich in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-14-2013, 04:31 AM
  5. [SOLVED] Range Selection and Active Cell problem
    By GazzaLDN in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2013, 07:47 AM
  6. Replies: 2
    Last Post: 12-23-2012, 09:26 AM
  7. How to add Range Selection of an Active Shape's Top/Bottom Row
    By ExcelInNeed in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2012, 08:38 PM

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