使用输入框添加新的 sheet,检查现有的 sheet 名称和无效的 sheet 名称

Add a new sheet using Input Box, check existing sheet names and invalid sheet names

我是 VBA 的新手,但我需要用它做点什么。我想制作一个输入框,添加一个具有特定名称的新 sheet。不知何故,我可以在论坛上搜索一番后成功。这是我想做的步骤,但我无法完全完成。

这是我目前使用的代码

Public Sub CariSheet()
Dim SheetName As String
Dim shExists As Boolean

Do

SheetName = InputBox("Write the name of sheet", "Add Sheet")
If NamaSheet <> "" Then

shExists = SheetExists(SheetName)
If Not shExists Then

Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = SheetName
MsgBox "The sheet " & (SheetName) & " is successfuly made", , "Result"
Else

MsgBox "The name is already exist, please enter a new name", vbOKOnly + vbInformation, "Name"

End If
End If
Loop Until Not shExists Or SheetName = ""
End Sub

Private Function SheetExists(ByVal SheetName As String, _
Optional ByVal wb As Workbook)

If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = Not wb.Worksheets(SheetName) Is Nothing

End Function

任何帮助将不胜感激,在此先感谢您的关注。啊,抱歉我的英语不好。

检查此代码是否对您有帮助:

刚刚为您添加了 Else 部分 Main If 条件,您检查 If Sheetname 是否为空

此外,如果您想在输入框为空的情况下退出子例程,您也可以取消注释我的行 Exit Sub

Public Sub CariSheet()
Dim SheetName As String
Dim shExists As Boolean

Do

SheetName = InputBox("Write the name of sheet", "Add Sheet")
If SheetName <> "" Then
shExists = SheetExists(SheetName)
    If Not shExists Then    
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = SheetName
        MsgBox "The sheet " & (SheetName) & " is successfuly made", , "Result"
    Else
        MsgBox "The name is already exist, please enter a new name", vbOKOnly + vbInformation, "Name"
    End If
Else
    MsgBox "Please enter a sheet name.", vbOKOnly + vbInformation, "Warning"
'Exit Sub
End If
Loop Until Not shExists Or SheetName = ""
End Sub

此代码可解决以下任一错误:

  1. sheet 名称已经存在
  2. sheet 名称无效(空(即“”)、太长或无效字符)

代码更新,以便 sheet 名称的长度得到验证,然后在创建 sheet 之前通过 Regexp 验证 Valid characters for Excel sheet names

如果 1 或 2 为真,将重新提示用户(附加重试消息)

Public Sub CariSheet()
Dim SheetName As String
Dim bFinished As Boolean
Dim strMsg As String
Dim ws As Worksheet

Do While Not bFinished
    SheetName = InputBox("Pls enter the name of the sheet", strMsg, "Add Sheet")
    On Error Resume Next
    Set ws = Sheets(SheetName)
    On Error GoTo 0

    If ws Is Nothing Then
        Select Case Len(SheetName)
        Case 0
        strMsg = "Sheet name is blank"
        Case Is > 31
        strMsg = "Sheet name exceeds 31 characters"
        Case Else
            If ValidSheetName(SheetName) Then
                Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
                ws.Name = SheetName
            Else
                strMsg = "Sheet name has invalid characters"
            End If
        End Select
    Else
        strMsg = "Sheet exists"
        Set ws = Nothing
    End If
Loop

End Sub

测试有效 sheet 名称

Function ValidSheetName(strIn As String) As Boolean
    Dim objRegex As Object
    Set objRegex = CreateObject("vbscript.regexp")
    objRegex.Pattern = "[\<\>\*\\/\?|]"
    ValidSheetName = Not objRegex.test(strIn)
End Function