Sorting where rows are dynamic.

gene7135

New Member
Joined
Feb 12, 2014
Messages
2
I'm new to Excel VBA. This seems like it should be simple though.

Here is my code.

Currently I get a 1004 error. "Method of Range of Object _Global Failed" on "Range(Selection).Sort.SortFields.Clear"

Any help is greatly appreciated.


Code:
Sub sortBacklog()

    Dim appExcel As Excel.Application
    Dim myWorkbook As Excel.Workbook
    Dim myWorkSheet As Worksheet
    
    
    Set appExcel = CreateObject("Excel.Application")
    Set myWorkbook = appExcel.Workbooks.Open("C:\Backlog.xls")
    Set myWorkSheet = myWorkbook.Sheets(1)
    
    myWorkSheet.Activate
    myWorkSheet.Select
    
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection).Sort.SortFields.Clear
      
       
  Range(Selection).Sort key1:=Range("F2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
    
    
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    appExcel.Quit
    Set myWorkSheet = Nothing
    Set myWorkbook = Nothing
    Set appExcel = Nothing


End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I am making progress.

Instead of using Clearing the sortfields from the Range Command I clear it from the Worksheet using
Code:
myWorkSheet.Sort.SortFields.Clear

Now I receive the same error from
Code:
Range(Selection).Sort key1:=Range("F2"), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,597
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top