View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Worksheet_Change - initiate macro on another worksheet

Assuming that the cells in G depend only on other cells in the same worksheet
then...

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrorHandler

If Not Intersect(Target, Range("G3:G110").Precedents) Is Nothing Then
Application.EnableEvents = False
Application.ScreenUpdating = False

With Sheets("Sheet2")
.Range("A1:C50").Sort Key1:=.Range("C2"), Order1:=xlDescending, _
Header:=xlYes
End With

End If
ErrorHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

If there are off sheet precidents then things get ugly.
--
HTH...

Jim Thomlinson


"oms" wrote:

I am trying to get excel to automatically run a macro based off a change in
any cell in a column. I've been trying worksheet_change but have not had
much luck.

Objective:
If there is a change to any of the cells in column G of Sheet 1 (cell
contains a formula that works off other cells), I want the macro to sort data
on Sheet 2 (info that I linked from Sheet 1). I am unhiding and hiding a
column in Sheet 2 because it contains the value that I'm sorting by but I
don't want it to show in the final product.

I've attached the code below. Thanks in advance.

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("G3:G110")) Is Nothing Then

Application.EnableEvents = False
Application.ScreenUpdating = False
On Error Resume Next

Sheets("Sheet 2").Select

'Unhide Column C
Columns("B:D").Select
Selection.EntireColumn.Hidden = False

'Sort based on Column C value
Range("C2").Select
Range("A1:C50").Sort Key1:=Range("C2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

'Hide Column C
Columns("C:C").Select
Selection.EntireColumn.Hidden = True

End If

Application.EnableEvents = True
Application.ScreenUpdating = True
On Error GoTo 0

Sheets("Sheet 1").Select

End Sub