我有一个代码,它会根据下拉列表中的单元格值重命名工作表。我有两个下拉列表" C21"和" G17"和按钮,将帮助我重命名工作表。
我担心的是,我需要禁用来自" H1"到" M40"这样用户在重命名工作表之前就无法输入数据。基本上如果按钮被成功执行,单元格应该是活动的,如果不是" NO"。你能帮我吗?
Sub RenameCurrentSheet()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim worksh As Integer
Dim worksheetexists As Boolean
Dim ws As Worksheet
worksh = Application.Sheets.Count
worksheetexists = False
ThisWorkbook.Unprotect Password:="xyz"
For x = 1 To worksh
If Worksheets(x).Name = ActiveSheet.Range("C21").Value & "-" & ActiveSheet.Range("G17").Value Then
worksheetexists = True
MsgBox ("Sheet with the name " + Worksheets(x).Name + " Already Exists!!!")
Exit For
End If
Next x
If worksheetexists = False Then
For x = 1 To worksh
If ActiveSheet.Name = "MainSheet" Then
MsgBox "You Cannot Change Name of This Sheet!!!"
Else
ActiveSheet.Name = Range("C21").Value & "-" & Range("G17").Value
Exit For
End If
Next x
End If
Application.DisplayAlerts = True
ThisWorkbook.Protect Password:="xyz"
End Sub
请帮助!!
答案 0 :(得分:0)
在用于测试工作表名称的代码中,您可以使用以下代码禁用单元格:
Range("H1:M40").Locked = True
然后当名称不再重复时,它将等于false。
注意这仅在工作表受到保护时才有效。
编辑:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Set myRng = Range("H1:M40")
For Each C In myRng
If C.Address = Target.Address And ActiveSheet.Name = ActiveSheet.Range("C21").Value & "-" & ActiveSheet.Range("G17").Value Then
ActiveSheet.Unprotect Password:= "xyz"
Range("H1:M40").Locked = False
ActiveSheet.Protect Password:= "xyz"
Exit For
Else
ActiveSheet.Unprotect Password:= "xyz"
Range("H1:M40").Locked = True
ActiveSheet.Protect Password:= "xyz"
End If
Next C
End Sub