VBA Macro issue.

El Scampio

New Member
Joined
Mar 27, 2009
Messages
12
Hi

Running Excel 2007 on XP.

I kindly was helped in recording a Macro for a simple sort function. The Macro will work initially but after that will constantly fail.

It takes me to debugging in VBA which I have no idea about.

This is the macro:

Sub Sort()
'
' Sort Macro
'

'
Range("C6:P106").Select
ActiveWindow.SmallScroll Down:=-111
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("P6:P106") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("C6:P106")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

The part that is Red and Bold is highlighted yellow on VBA so I gather that is where the problem lies.

Basically all I want the Macro to do is highlight cells: C6 to P106 and sort from largest to smallest by column P.

I have re-recorded the macro in hope it would fix it but the same issue repeats every time.

Any help would be greatly appreciated.

Thanks

Scamp
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Scamp,

2 possible causes (maybe others that I haven't thought of)

1) The name of the sheet has been changed, Sheet1 is now called something else.

2) Sheet 1 is not the active sheet (the sheet that is visible when you run the macro). Your macro looks as if it should run from any sheet, but I've had the same problem previously.

If that doesn't solve it, what error message are you getting?
 
Upvote 0
Scamp,

2 possible causes (maybe others that I haven't thought of)

1) The name of the sheet has been changed, Sheet1 is now called something else.

2) Sheet 1 is not the active sheet (the sheet that is visible when you run the macro). Your macro looks as if it should run from any sheet, but I've had the same problem previously.

If that doesn't solve it, what error message are you getting?

Thank you you nailed it.

I had remaned the worksheet from "Sheet1" to "Planner". I have changed the "Sheet1" parts to "Planner" and now works great.

Thanks for you help.
 
Upvote 0
I couldn't leave you with that nasty recorded macro, this might not be the best but it's an improvement :)

Code:
Sub Sort()
Worksheets("Planner").Select
Range("C6:P106").Select
Selection.Sort Key1:=Range("P6"), Order1:=xlDescending, MatchCase:=False, _
Header:=xlGuess, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End Sub

Will you always be using C6:P106, or does the range vary in size?

Jason
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

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