Macro not selecting a cell when I tell it to!!?

L

Legacy 102432

Guest
I have this code which I am using to look out for a change in my dropdown list so that certain rows hide and unhide.

It works fine and I have used this code many other times and then adapted with different range names etc. to suit.

The only trouble is that it is not going back to the "Name" cell in the first elseif, and the last elseif. The macro seems to finish when it has selected my rows range.

Any ideas why it is doing this???




Code in Sheet1 to pick up the worksheet change of my dropdown cell

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'When the user makes a selection from the "StaffTypeCell" then a macro will be run to unhide
'required sections of the form
On Error Resume Next
If Target.Address(0, 0) = "P12" Then
    Application.EnableEvents = False
    Call FormUnhide
    Application.EnableEvents = True
End If
On Error GoTo 0
End Sub

Code that unhides/hides the form when needed
Code:
Sub FormUnhide()
'
'On Error GoTo ErrorHandler:
    With Application
        .EnableCancelKey = xlDisabled
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    If Range("StaffTypeCell") = "" Then
    Range("StaffTypeCell").Value = "Select Staff Type"
    'This macro unhides the required rows when PERMANENT STAFF is selected
    ElseIf Range("StaffTypeCell").Value = "Permanent Staff" Then
        ActiveSheet.Unprotect Password:="23"
        'Unhides the area of the form for Permanent Staff
        Range("Rows_PermanentEmployee").Select
        Selection.EntireRow.Hidden = False
        'Hides the rows at the top of the form where the select staff type button is
        Range("Rows_Button").Select
        Selection.EntireRow.Hidden = True
        'Ensures the bottom section of the form is hidden
        Range("Rows_BottomForm").Select
        Selection.EntireRow.Hidden = True
        'Hides the row that asks how many hours are to be deducted from Payroll
        'as this question is only relevant to Fixed Term Employees
        Range("Rows_UnpaidLeave_PayrollHours").Select
        Selection.EntireRow.Hidden = True
        'Selects the name cell
        Range("Name").Select
        'Password protects the sheet
        ActiveSheet.Protect Password:="123"
        With Application
            .EnableCancelKey = xlInterrupt
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    'This macro hides the form when "Select Staff Type" is selected
    ElseIf Range("StaffTypeCell") = "Select Staff Type" Then
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect Password:="123"
        'Hides all of the form except the button rows
        Range("Rows_AllForm").Select
        Selection.EntireRow.Hidden = True
        'Selects the Staff Type dropdown
        Range("StaffTypeCell").Select
        ActiveSheet.Protect Password:="123"
        With Application
            .EnableCancelKey = xlInterrupt
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    'This macro unhides the form
    Else
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect Password:="123"
        'Unhides the area of the form for Permanent Staff
        Range("Rows_PermanentEmployee").Select
        Selection.EntireRow.Hidden = False
        'Hides the rows at the top of the form where the select staff type button is
        Range("Rows_Button").Select
        Selection.EntireRow.Hidden = True
        'Ensures these rows are showing as they are relevant to Fixed Term Employees only
        Range("Rows_UnpaidLeave_PayrollHours").Select
        Selection.EntireRow.Hidden = False
        'Ensures the bottom section of the form is hidden
        Range("Rows_BottomForm").Select
        Selection.EntireRow.Hidden = True
        'Hides the Special Paid Leave section
        Range("Rows_SpecialPaidLeave").Select
        Selection.EntireRow.Hidden = True
        'Selects the Name cell
        Range("Name").Select
        'Password protects the sheet
        ActiveSheet.Protect Password:="123"
        
        With Application
            .EnableCancelKey = xlInterrupt
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End If
'Exit to avoid error handler.
Exit Sub
'ErrorHandler:
'MsgBox "An error has occured." _
'& vbCrLf & "Please report this error." _
'& vbCrLf & "We will investigate and try to resolve this issue." _
'& vbCrLf & "Thank You."
'
'    With Application
'        .EnableCancelKey = xlInterrupt
'        .EnableEvents = True
'        .ScreenUpdating = True
'    End With
'
'ThisWorkbook.Close False
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,203,205
Messages
6,054,136
Members
444,703
Latest member
pinkyar23

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