View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Event Driven Procedure

Looks like I misread your OP and am formatting red on the wrong sheet

change
Set ws = Me.Worksheets(1)

to
Set ws = Me.Worksheets(2) ' or use "sheet-name" iso 2


Sheet hide/unhide does not of itself trigger an event. However it normally
(always if done manually) it has the effect of activating or deactivating a
sheet in the activeworkbook. The simplest way is to trap the activate event
of all sheets at workbook level and format the cell as required, but only if
necessary (see note re preserving undo).

In the Workbook_SheetActivate (in the ThisWorkbook module) you could indeed
call a routine in an ordinary module to do the work, no need to pass the
activated sheet object in this case but change "Me" to ThisWorkbook. There's
good reason to minimise code in "object" modules but for this simple routine
probably not much point to move it elsewhere.

If you don't want any code at all in sheet/Thisworkbook modules you can trap
events in a class module using "WithEvents", you can trap events of any or
all other workbooks so your code could go in (say) an addin or your
Personal.

Regards,
Peter T

"Varne" wrote in message
...
Hi!

Sorry for responding late!

Worksheets(1).cells(1,1) goes red when Worksheets(2) in unhidden. However
when Worksheets(2) is hidden the red remains. As I am not familiar with
your
coding style my adjustments did not work.

Also I found something else;

Usually I keep the procedures in Modules and made them called (For example
when a sheet gets activated) on events. Your codes could not be handled
that
way.

Please reply.

Thanks.

"Peter T" wrote:

Paste the following in the ThisWorkbook module

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim cidx As Long
Dim ws As Worksheet

Set ws = Me.Worksheets(1)

With ws.Range("A1").Interior
cidx = .ColorIndex
' only apply if necessary to avoid loss of undo
If ws.Visible = xlSheetVisible Then
If cidx < 3 Then .ColorIndex = 3
ElseIf cidx < xlNone Then
.ColorIndex = xlNone
End If
End With

End Sub

Post back you are checking more than one cell, would need to change
slightly.

Regards,
Peter T

"Varne" wrote in message
...
Hi!

In an Excel workbook with 2 pages the 2nd is hidden. If it is unhidden
the
following Macro should be triggered;

Sub Test ()

Worksheets(1).Cells(1,1).Interior.Colorindex=3

End Sub

Could someone help?

Thank You!