VBA Worksheet_Change Help (might be a simple thing)

chooch147

New Member
Joined
Mar 25, 2005
Messages
12
I'm not a VBA or Excel expert, but I thought the code below was pretty simple. It basically sorts a range of cells everytime a value on the sheet changes. It worked perfectly in Excel 2003, but in Excel 2007 nothing happens. Does anyone have any idea if there is an incompatibility, or maybe something in 2007 that I need to enable for this to work?

Private Sub Worksheet_Change(ByVal Target As Range)
With Worksheets("LeaderBoard")
.Range("C4:E28").Sort Key1:=.Range("D4"), Order1:=xlDescending, Key2:=.Range("E4"), Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Range("H4:J28").Sort Key1:=.Range("I4"), Order1:=xlDescending, Key2:=.Range("J4"), Order2:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
It should be backwards compatible, but you might want to try recording the sort in 2007 and replacing the resulting code.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
54,312
Office Version
  1. 365
Platform
  1. Windows
It works for me in both Excel 2002 and Excel 2007. Two things for you to check:

1. When you open the workbook in Excel 2007, you will need to ensure macros are enabled. Depends on your macro security settings, but when you open the workbook have look just above the formula bar for a 'Security Warning'. If it is there, you will need to click 'Options...' and choose 'Enable this content'

2. If the above doesn't fix the problem, you also need to ensure that your vba 'Events' have not been disabled. In the VB window, ensure the Immediate Window is visible (View|Immediate Window) and then on a new line in the Immediate Window, type
Application.EnableEvents=True and press Enter
 

chooch147

New Member
Joined
Mar 25, 2005
Messages
12
Smitty...thanks for the advice. I tried that but it didn't work.

Peter -- you were dead on. I already had macros enabled, but did not have the VBA Events enabled. Like I said in the post, I am FAR from an expert so I had no idea where to look for this.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,983
Messages
5,834,709
Members
430,311
Latest member
JAC0617

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
Top