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
 

Smitty

MrExcel MVP
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
42,563
Office Version
365
Platform
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.
 

Forum statistics

Threads
1,084,889
Messages
5,380,446
Members
401,679
Latest member
saffar

Some videos you may like

This Week's Hot Topics

Top