?
Solved

Convert Recorded Macro Selections to Current Selection Excel 2007

Posted on 2012-09-10
6
Medium Priority
?
340 Views
Last Modified: 2012-09-11
Here's a recorded macro. I'm having no luck changing the selection(s) to "active selection". I think the search terms required to get an answer are simply too generic.

Bottom line: I want to sort the active selection without being prompted to extend it. Sort A-->Z. I will run the macro from a shape on the worksheet, so I just need a standard sub. Thanks soooo much!!

Sub Macro3()
'
' Macro3 Macro
'

'
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G5"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("G5:G11")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Open in new window

0
Comment
Question by:Dreamboat
  • 2
  • 2
  • 2
6 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 1000 total points
ID: 38383392
Do you want to sort on the first field of your selection? Try this:

Sub Macro3()
'
' Macro3 Macro
'

'
    activesheet.Sort.SortFields.Clear
activesheet.Sort.SortFields.Add Key:=selection.cells(1,1), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With activesheet.Sort
        .SetRange selection
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
                                  

Open in new window

0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 1000 total points
ID: 38383393
I would think:
Sub Macro3()
'
' Macro3 Macro
'

'
    With ActiveSheet.Sort
        With .SortFields
            .Clear
            .Add Key:=Selection.Cells(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        End With
        .SetRange Selection
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Open in new window


should do it
0
 
LVL 39

Expert Comment

by:nutsch
ID: 38383405
photo finish.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 38383417
:)
0
 
LVL 22

Author Comment

by:Dreamboat
ID: 38384981
LOL. I'll check it out tomorrow guys! Thanks so much. :D
0
 
LVL 22

Author Closing Comment

by:Dreamboat
ID: 38386427
Beauteous! Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question