我有A2到A20的细胞 想要在该范围内的单元格值发生变化时生成新的工作表。
此外,生成的新工作表将重命名为已更改单元格的值。
我让这段代码正常工作(针对单个单元格),直到用户请求范围
option.getCallback().process(sort); // sort is the reference to this
End Sub
答案 0 :(得分:2)
一旦Range("A2:A20")
内的值发生变化,新工作表名称等于单元格值,下面的代码就会创建一个新的工作表。
该代码还验证没有具有该名称的退出表(将导致错误)。
<强>代码强>
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim ws As Worksheet
Dim lastrow As Long
' you are not doing anything currently with the last row
'lastrow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row + 1
' according to your post you are scanning Range A2:A20 (not B5)
Set KeyCells = Range("A2:A20")
If Not Intersect(KeyCells, Target) Is Nothing Then
For Each ws In Worksheets
' if sheet with that name already exists
If ws.Name = Target.Value Then
MsgBox "A Worksheet with Cell " & Target.Value & " already exists"
Exit Sub
End If
Next ws
Set ws = Worksheets.Add
ws.Name = Target.Value
End If
End Sub