Cell selection altered after Worksheet Activate code ends

John W

New Member
Joined
Feb 24, 2014
Messages
3
I have four worksheets for storing diferent types of data, all four sheets have identical code attached.
The sheets are protected and maybe filtered.
The code is intended to stop the user selecting anywhere except column 1 on any row after row 3. Column 1 can be used for selecting that row as input to a data entry form.
Only one of the sheets does not work correctly, The selection is always moved to row 4 column 1 when the activate code has ended.
I have single stepped through the VBA code.

Tax data.xlsm
ABCDEFGHIJL
106/04/2020 Mon1416,330.00190.042
206/04/2019 Sat1776,320.00415.5535
3DateNameFeeHoursStartVenueFinishDistance1Distance2JourneysNotes
407/03/2016 MonAccounts#3.00
524/03/2016 ThuAccounts#3.00
606/04/2016 WedAccounts#8.00
706/04/2016 WedSophie Timms40.001.5TN33 0LHTN33 0DETN33 0LH332
812/04/2016 TueAlice Gildea40.001.0TN33 0LHTN33 0DETN33 0LH332
915/04/2016 FriKenneth Fox35.001.00
1016/04/2016 SatAlison Norris35.001.00
1116/04/2016 SatCaroline Taylor60.001.5TN33 0LHTN34 1BATN33 0LH992
1216/04/2016 SatKristal Seidler40.001.00
1316/04/2016 SatPractice#2.00
1423/04/2016 SatBethany Williams35.001.00
1523/04/2016 SatKenneth Fox35.001.00
1623/04/2016 SatPractice#2.00
Student
Cell Formulas
RangeFormula
A1A1=IF(TODAY() > DATE(YEAR(TODAY()),4,5),DATE(YEAR(TODAY()),4,6),DATE(YEAR(TODAY())-1,4,6))
B1B1=COUNTIFS(Student!$A4:$A10000, ">=" & $A1,Student!$A4:$A10000, "<=" & DATE(YEAR($A1)+1,4,5),Student!$C4:$C10000,"<>#")
C1:D1C1=SUMIFS(Student!C3:Student!C10000,Student!$A3:Student!$A10000, ">=" &$A1,Student!$A3:Student!$A10000, "<=" &DATE(YEAR($A1)+1,4,5))
A2A2=DATE(YEAR(A1)-1,4,6)
B2B2=COUNTIFS(Student!$A4:$A10000, ">=" & $A2,Student!$A4:$A10000, "<=" & DATE(YEAR($A2)+1,4,5),Student!$C4:$C10000,"<>#")
C2:D2C2=SUMIFS(Student!C3:Student!C10000,Student!$A3:Student!$A10000, ">=" &$A2,Student!$A3:Student!$A10000, "<=" &DATE(YEAR($A2)+1,4,5))
J1J1=SUMPRODUCT(--(A4:A10000>=A1),--(A4:A10000<DATE(YEAR(A1)+1,4,6)),(H4:H10000*J4:J10000))+SUMPRODUCT(--(A4:A10000>=A1),--(A4:A10000<DATE(YEAR(A1)+1,4,6)),(I4:I10000*J4:J10000))
J2J2=SUMPRODUCT(--(A4:A10000>=A2),--(A4:A10000<DATE(YEAR(A2)+1,4,6)),(H4:H10000*J4:J10000))+SUMPRODUCT(--(A4:A10000>=A2),--(A4:A10000<DATE(YEAR(A2)+1,4,6)),(I4:I10000*J4:J10000))


The worksheet code is as follows
VBA Code:
Private Sub Worksheet_Activate()
   If Sheet_Rebuild = True Then Exit Sub
   Setup_OnKey
   Select_Column1
End Sub

Private Sub Worksheet_Deactivate()
   If Sheet_Rebuild = True Then Exit Sub
   Clear_OnKey
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   If Sheet_Rebuild = True Then Exit Sub
   If ActiveSheet.ProtectContents = False Then Exit Sub
   If First_Pass = False Then
       First_Pass = True
   Else
       Worksheet_Selection
   End If
End Sub

Sub Select_Column1()
    rw = ActiveCell.Row
    cl = ActiveCell.Column
    If rw >= FirstRow And cl > 1 Then
        cl = 1
        Cells(rw, cl).Select
    End If
End Sub


Routines called are:
Sub Sim_Key_Tab()
    Simulate_Keys ("tab")
End Sub
Sub Sim_Key_down()
    Simulate_Keys ("down")
End Sub
Sub Sim_Key_up()
    Simulate_Keys ("up")
End Sub
Sub Sim_Key_left()
    Simulate_Keys ("left")
End Sub
Sub Sim_Key_right()
    Simulate_Keys ("right")
End Sub

Sub Setup_OnKey()

    Application.OnKey Key:="{TAB}", Procedure:="Sim_Key_Tab"
    Application.OnKey Key:="{down}", Procedure:="Sim_Key_down"
    Application.OnKey Key:="{UP}", Procedure:="Sim_Key_up"
    Application.OnKey Key:="{LEFT}", Procedure:="Sim_Key_left"
    Application.OnKey Key:="{Right}", Procedure:="Sim_Key_right"
   
End Sub

Sub Clear_OnKey()

    Application.OnKey Key:="{tab}"      ', Procedure:=""
    Application.OnKey Key:="{down}"     ', Procedure:=""
    Application.OnKey Key:="{up}"       ', Procedure:=""
    Application.OnKey Key:="{left}"     ', Procedure:=""
    Application.OnKey Key:="{right}"    ', Procedure:=""
   
End Sub

Sub Simulate_Keys(KeyName As String)
    If Trace_Sw Then Debug_Print ("Simulate_Keys~" & KeyName)

    wn = ActiveSheet.Name
    rw = ActiveCell.Row
    cl = ActiveCell.Column
   
    If Worksheets(wn).ProtectContents = True Then
        cl = 1
        Select Case LCase(KeyName)
            Case "tab", "down", "right"
                rw = rw + 1
                If rw < 3 Then rw = 3
            Case "up", "left"
                rw = rw - 1
                If rw < 3 Then rw = 3
        End Select
    Else
        Select Case LCase(KeyName)
            Case "tab"
                cl = cl + 1
            Case "down"
                rw = rw + 1
            Case "right"
                cl = cl + 1
            Case "up"
                rw = rw - 1
                If rw < 1 Then rw = 1
            Case "left"
                cl = cl - 1
                If cl < 1 Then cl = 1
        End Select
    End If
    Worksheets(wn).Cells(rw, cl).Select
   
End Sub
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
If you are using sheet protection just change the cells you want them to access as unlocked then when protecting dont allow selection of locked cells? Could that work??
 
Upvote 0
That was my first idea, but unfortunately that allows the user to change the data, I only want them to be able to select the row, not to change it.
 
Upvote 0
On further investigation I have found the cause. Code which I had previously added at the workbook level was causing the problem but was only doing it for one sheet as It was incorrcetly identifying the other 3 sheets, but what I still don't understand is why the execution of the code was not visible as I was stepping through the code.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top