Macro to sort Named Range not working

Shiremaid

Board Regular
Joined
Jun 18, 2013
Messages
73
I am using a macro to add a row to a named range and then sort the range into alphabetical order.
I have code on another sheet where it's working, but I just cannot get it to work on the second one, and I don't know why.
I tried naming the cell I want to use as sorting criteria (Timing_S) but that didn't help. I even tried recording the sort macro and using the code from that and it didn't work (and included a lot of ActiveWorkbook stuff that I don't usually see and I only had one workbook open)
Help?

This is the code that's giving me grief
Code:
Range("Timing").Select
Selection.Offset(2, 0).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Range("A3:T3").Borders.LineStyle = xlContinuous
ActiveCell.Range("A2:T2").Select
Selection.Autofill Destination:=ActiveCell.Range("A1:T2"), Type:= _
        xlFillDefault
Selection.Replace What:="SiteTemplate", Replacement:="yule", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("Timing").Offset(0, 1).Select
    Range("Site_Report").Sort key1:=ActiveCell, Header:=xlYes

This is the code that's working
Code:
    Range("CountryAdd2").Select
    Dim existing As String
    existing = ActiveCell.Offset(7, 0).Value
    
    Range("CountryAdd2").Offset(7, 0).EntireRow.Select
    ActiveCell.Rows("1:4").EntireRow.Select
    Selection.Insert Shift:=x1Down, CopyOrigin:=xlFormatFromRightOrBelow


    ActiveCell.Offset(4, 0).Range("A1:H4").Select
    Selection.Autofill Destination:=ActiveCell.Offset(-4, 0).Range("A1:H8"), Type _
            :=xlFillDefault
    ActiveCell.Offset(-4, 0).Range("A1:H4").Select
    
    Selection.Replace What:=existing, Replacement:=country, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False


    Range("CountryAdd2").Offset(0, 6).Select
    Range("Site_Report").Sort key1:=ActiveCell, Header:=xlYes
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
When you say "range" I assume that range is a single column because you have to specify a sort criteria for a column.

You say it doesn't work, what does it do?

If there is an error, what is the error? and on what line?

(Nobody can run this code for you without the worksheet with the named ranges)
 
Upvote 0
Range("Timing").Offset(0, 1).Select
Range("Site_Report").Sort key1:=ActiveCell, Header:=xlYes
The only thing I can see is that if your ActiveCell is not in the Range("Site_Report") it would fail.
 
Upvote 0
CountryAdd2 and Timing are named cells.
Site_Report and Timing_Metrics (what the last line in the trouble should read where it says Site_Report, but fixing that didn't help) are named ranges spanning multiple rows and cells.

I get an error at the last line. Run-time error'1004': _ Methis 'Range' of object '_Global' failed.

....aaand I just double checked my Range in Name Manager and my problem was I defined it wrong. It works now. (head/desk)
Sigh. I hate when that happens.
Thanks for the replies.
 
Last edited:
Upvote 0
CountryAdd2 and Timing are named cells.
Site_Report and Timing_Metrics (what the last line in the trouble should read where it says Site_Report, but fixing that didn't help) are named ranges spanning multiple rows and cells.

I get an error at the last line. Run-time error'1004': _ Methis 'Range' of object '_Global' failed.

....aaand I just double checked my Range in Name Manager and my problem was I defined it wrong. It works now. (head/desk)
Sigh. I hate when that happens.
Thanks for the replies.
Not the only horse in that barn. Thanks for the feedback.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,203,687
Messages
6,056,747
Members
444,888
Latest member
Babi_mn

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