|
サンプルです。
Sub test()
Dim wb As Workbook
Dim basewsmei As String
Dim wsmei As String
Dim flg As Boolean
Dim newwsmei As String
Dim cnt As Integer
Set wb = ActiveWorkbook
basewsmei = "Sheet"
If wschek(wb, basewsmei) = False Then
newwsmei = basewsmei
Else
cnt = 0
Do Until flg = True
cnt = cnt + 1
newwsmei = basewsmei & "(" & Format(cnt, "0") & ")"
If wschek(wb, newwsmei) = True Then
flg = False
Else
flg = True
End If
Loop
End If
wb.Worksheets.Add after:=wb.Worksheets(wb.Worksheets.Count)
ActiveSheet.Name = newwsmei
Set wb = Nothing
End Sub
Function wschek(ByVal wb As Workbook, wsmei As String) As Boolean
Dim myrng As Range
Err.Clear
On Error Resume Next
Set myrng = wb.Worksheets(wsmei).Range("A1")
If Err.Number <> 0 Then
wschek = False '存在しない=新規シート名として使える
Else
wschek = True '存在する=新規シート名として使えない
End If
Set myrng = Nothing
On Error GoTo 0
End Function
|
|