Hi,
I would like to run macro when cell D4 is changed by dropdown box (rename the sheet to the value in D4 & run a filter to hide rows).
But the code I have tried to 'put' together works when any cell is changed on the sheet which is slowing down data entry. Kindly assist keeping in mind that i'm a novice. Thanks
CODE:
Private Sub Worksheet_Change(ByVal Target As Range)
'Renames sheet to value in D4
Set Target = Range("D4")
If Target = "" Then Exit Sub
On Error GoTo ErrHandler1:
Application.ActiveSheet.Name = VBA.Left(Target, 31)
'Registers password name
'Sets sheet password
sheetpassword = "horse"
'Sets Sheet Name variable
Sheetname = ActiveSheet.Name
'unlock sheet - must specify sheet name
Worksheets(Sheetname).Unprotect Password:=sheetpassword
'Hides rows
Range("A6:AP1007").Select
Selection.AutoFilter
ActiveSheet.Range("$A$6:$AP$1007").AutoFilter Field:=1, Criteria1:="<>"
'locks the sheet
Worksheets(Sheetname).Protect Password:=sheetpassword
Exit Sub
ErrHandler1: 'incase of error
MsgBox ("Your sheet name was unable to be renamed as you have another sheet with the same name, rename please")
Exit Sub
End Sub
I would like to run macro when cell D4 is changed by dropdown box (rename the sheet to the value in D4 & run a filter to hide rows).
But the code I have tried to 'put' together works when any cell is changed on the sheet which is slowing down data entry. Kindly assist keeping in mind that i'm a novice. Thanks
CODE:
Private Sub Worksheet_Change(ByVal Target As Range)
'Renames sheet to value in D4
Set Target = Range("D4")
If Target = "" Then Exit Sub
On Error GoTo ErrHandler1:
Application.ActiveSheet.Name = VBA.Left(Target, 31)
'Registers password name
'Sets sheet password
sheetpassword = "horse"
'Sets Sheet Name variable
Sheetname = ActiveSheet.Name
'unlock sheet - must specify sheet name
Worksheets(Sheetname).Unprotect Password:=sheetpassword
'Hides rows
Range("A6:AP1007").Select
Selection.AutoFilter
ActiveSheet.Range("$A$6:$AP$1007").AutoFilter Field:=1, Criteria1:="<>"
'locks the sheet
Worksheets(Sheetname).Protect Password:=sheetpassword
Exit Sub
ErrHandler1: 'incase of error
MsgBox ("Your sheet name was unable to be renamed as you have another sheet with the same name, rename please")
Exit Sub
End Sub