Sort Method of Range Class Failed

iwantpie

New Member
Joined
Aug 1, 2006
Messages
13
Hi all

First off just want to say what a great helpful community you have here. I hardly ever post questions mostly because I don't need to due to the help you have given out in the past but this time I am stumped on something. The kind of problem I'm about to describe is something I occasionally run into and can never quite make head nor tails out of so I'm hoping you can help.

I'm building a tool that contains some HR elements and one of those is absence monitoring. Currently I'm writing the code for tidying up all the different sheets when somebody leaves and that includes finding and removing them from three separate absence monitoring locations. The code I'm using for this is below.

Code:
lRow = Sheets("Absence Input").Range("C:C").Find(strName, LookIn:=xlValues).Row
lEmptyRow = Sheets("Absence Input").Cells(Rows.Count, 3).End(xlUp).Row + 1
Sheets("Absence Input").Rows(lRow).ClearContents
Sheets("Absence Input").Rows(lEmptyRow).Copy
Sheets("Absence Input").Paste Destination:=Sheets("Absence Input").Cells(lRow, 1)
Sheets("Absence Input").Range("C13:AXK200").Sort key1:=Sheets("Absence Input").Range("C13:C200"), order1:=xlAscending, Header:=xlNo

lRow = Sheets("Staff Entitlements Report").Range("B:B").Find(strName, LookIn:=xlValues).Row
lEmptyRow = Sheets("Staff Entitlements Report").Cells(Rows.Count, 2).End(xlUp).Row + 1
Sheets("Staff Entitlements Report").Rows(lRow).ClearContents
Sheets("Staff Entitlements Report").Rows(lEmptyRow).Copy
Sheets("Staff Entitlements Report").Paste Destination:=Sheets("Staff Entitlements Report").Cells(lRow, 1)
Sheets("Staff Entitlements Report").Range("B6:B200").Sort key1:=Sheets("Staff Entitlements Report").Range("B6:B200"), order1:=xlAscending, Header:=xlNo

lRow = Sheets("Monthly Absence Report").Range("B:B").Find(strName, LookIn:=xlValues).Row
lEmptyRow = Sheets("Monthly Absence Report").Cells(Rows.Count, 2).End(xlUp).Row + 1
Sheets("Monthly Absence Report").Rows(lRow).ClearContents
Sheets("Monthly Absence Report").Rows(lEmptyRow).Copy
Sheets("Monthly Absence Report").Paste Destination:=Sheets("Monthly Absence Report").Cells(lRow, 1)
Sheets("Monthly Absence Report").Range("B7:C200").Sort key1:=Sheets("Monthly Absence Report").Range("B7:B200"), order1:=xlAscending, Header:=xlNo

As you can see I've kept it almost identical for the three sheets with only small difference to the actual ranges being sorted and the ranges being used to locate the rows to be affected. For each sheet the code heads off to find the row with the leavers' name in it and an empty row from the bottom of the table (these rows contain some formatting which is why I do this). The leavers' row is then cleared of contents and the formatting from the empty row pasted on top. Then the whole sheet is sorted so the newly fresh row is shunted off down the bottom.

This whole process is triggered from a form with a button control that launches the macro in a fourth sheet. When the macro is triggered from this fourth sheet the first section runs fine (the one affecting the Absence Input Sheet) however I get the error from the title when it tries to sort either of the other two (I've swapped them around to make sure that it is both of them it stumbles with and it is). When I run the macro from inside the Visual Basic window it runs fine or if I run it from the button but step into it and manually switch over to the right sheets when it becomes time to sort it runs fine. I am assuming therefore that when the macro is called from the button it is encountering a problem switching to the relevant sheets to do the sort. What I don't understand is why. Originally I had this code in a With Sheets loop but replaced that with specific SHeets references to try and combat this problem with no luck.

Any help would be appreciated on this, I can't work out why it works fine for the Absence Input Sheet but not for the other two when the code is so similar.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Not sure why you are having a problem with the sort, but you only need one cell reference for your SortKey1 value.

Code:
Sheets("Absence Input").Range("C13:AXK200").Sort key1:=Sheets("Absence Input").[COLOR=#FF0000]Range("C13"[/COLOR]), order1:=xlAscending, Header:=xlNo

Sheets("Staff Entitlements Report").Range("B6:B200").Sort key1:=Sheets("Staff Entitlements Report").[COLOR=#FF0000]Range("B6"), [/COLOR]order1:=xlAscending, Header:=xlNo

Sheets("Monthly Absence Report").Range("B7:C200").Sort key1:=Sheets("Monthly Absence Report").[COLOR=#FF0000]Range("B7"), [/COLOR]order1:=xlAscending, Header:=xlNo
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

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