VBA增加sheet,重名时自动命名
VBA增加sheet,如何在出现重名时自动命名?如
Sheets.Add
ActiveSheet.Name = "mysheet"
如果已经存在名称为“mysheet”的sheet,如何使增加的sheet名称自动加上一个括号变成"mysheet(1)"呢? 即类似在某文件夹内不断复制同一文件的效果,代码如下:
Sub addsheets()
Dim i As Long, sh As String, temp As String, mysheet As Worksheet
Application.DisplayAlerts = False
Randomize
sh = InputBox("Please enter the newsheetname", , "sheet" & Int(Rnd * 6 + 1))
On Error GoTo here
For i = 0 To Sheets.Count
temp = sh & IIf(i = 0, "", "(" & i & ")")
Set mysheet = Sheets(temp)
Next
here:
Sheets.Add.Name = temp
Application.DisplayAlerts = True
End Sub