View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Workbook_SheetChange won't run

Where have you got that code. Based on the format it shouwl be in
ThisWorkBook. Even then it is not going to work but it should be throwing an
error. Try this...
Right click the XL icon in the upper left corner of the screen next to File
and select view code. Paste the following...

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo ErrorHandler
Application.EnableEvents = False
With Sheets("Sheet3") '***change me***
.Range("PatientNamesRooms").Sort Key1:=.Range("A2"), Order1:=xlAscending, _
Header:=xlYes
End With
ErrorHandler:
MsgBox Err.Description
Application.EnableEvents = True
End Sub

Note that you need to change the sheet reference to the sheet that the named
range is on. That is because even though the named range is global code in
the ThisWorkbook module acts on the active sheet which may not be the sheet
you want sorted... Also I changed your sor a little bit to get rid of the
xlGuess parameter. You should use xlYes or xlNo as they are less prone to
mistakes.
--
HTH...

Jim Thomlinson


"XL Novice DD..." wrote:

My understanding of Workbook_SheetChange is that with ANY change ANYWHERE in
the workbook, the code runs, correct? I can't get it to work and don't know
why:

GOAL: Resort the "PatientNamesRooms" (just a list of about 60 names and
associated hospital room numbers) whenever a change is made on a different
tab within the workbook. Cells in "PatientNamesRooms" are index formulas
that get names based on room numbers from a main roster of names. We need to
quickly find the name alphabetically to determine what room they're in.

I've gotten the code to run (names to sort) using Worksheet SelectionChange.
But that doesn't meet my need. It needs to resort anytime someone updates a
name in the roster (like when a patient leaves or is admitted on the floor).

CODE THAT WON'T RUN:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
Range("PatientNamesRooms").Select Selection.Sort Key1:=Range("A1"),
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
DataOption1:=xlSortNormal

End Sub

Help?