Originally Posted by
Paul_Hossler
Mark -- got the API working, but it had some side effects tha I couldn't overcome
1. It spends all it's time in a tight Do Loop
2. If I switch to the VBE, the loop is still running and I have to stop the macro
I'll keep the code, but for now I think I'll have to use the transparent rectangle and it's OnClick event
Thanks
Paul
Hi Paul,
I am definitely not the person to be speaking reference Class Modules and/or RaiseEvent, just barely getting my feet wet (probably just a toe or two) so far. That said (and Ken or others, please correct anything I get wrong or less than clear), I believe that the continuous running is needed for RaiseEvent as we are creating our own event.
I took it from your first post that double-click would not be of interest, as you don't want the calls for "why can't I select anything", but just to mention...
Excel's warning only comes up if the user double-clicks. If I leave the 'Protect worksheet and contents of locked cells' box ticked (in the 'Protect Sheet' dialog) and un-tick all boxes in the 'Allow all users of this worksheet to:' area, then, this seems to substitute for Excel's msg.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox "Sorry, no editing on this sheet...", vbInformation, vbNullString
End Sub
Now if the single-click would seem much better, but seems less loveable because of the continuous running, how about a couple of mods?
In ThisWorkbook, the code as shown in post 6 in the link, except:
Change these to Public Functions.
Public Function SetClickEvent(TargetSheet As Worksheet)
Public Function StopClickEvent()
This will keep the StopClickEvent from showing up in the run macro dialog.
Then ditch both the workbook open and close events, and add this to the worksheet's module:
Option Explicit
Public WithEvents Worksheet_ As ThisWorkbook
Private Sub Worksheet_Activate()
Call ThisWorkbook.SetClickEvent(TargetSheet:=Me)
End Sub
Private Sub Worksheet_Deactivate()
Call ThisWorkbook.StopClickEvent
End Sub
Private Sub Worksheet__Click(ByVal Target As Range)
'// Msg or warning to suit... //
MsgBox "Seriously, no editing on this sheet...", vbInformation, vbNullString
End Sub
ACK! Sorry, a tad out of order, but just thought to test. In case the sheet of interest is the last active sheet when the wb is saved/closed, the Workbook_Open could be:
Private Sub Workbook_Open()
If TypeName(ActiveSheet) = "Worksheet" Then
If ActiveSheet.CodeName = "Sheet1" Then
Call SetClickEvent(Sheet1)
End If
End If
End Sub
The transparent rectangle certainly sounds nice as well, especially if scrolling is limited. I just thought to suggest the above in case it sounds niftier w/o the code running all the time.
Mark