Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 代码以添加图纸并重命名该图纸_Excel_Vba - Fatal编程技术网

Excel 代码以添加图纸并重命名该图纸

Excel 代码以添加图纸并重命名该图纸,excel,vba,Excel,Vba,代码以添加工作表并从用户处重命名该工作表 Sub tenloops1() Worksheets.Add Sheets(ActiveSheet.Name).Select = InputBox("Enter Sheet Name") End Sub 我是VBA新手,希望代码符合您的要求 Sub tenloops1() Worksheets.Add ActiveSheet.Name = InputBox("Enter Sheet

代码以添加工作表并从用户处重命名该工作表

Sub tenloops1()
    
    Worksheets.Add
    
    Sheets(ActiveSheet.Name).Select = InputBox("Enter Sheet Name")

End Sub

我是VBA新手,希望代码符合您的要求

Sub tenloops1()

Worksheets.Add

ActiveSheet.Name = InputBox("Enter Sheet Name")

End Sub

我是VBA新手,希望代码符合您的要求

Sub tenloops1()

Worksheets.Add

ActiveSheet.Name = InputBox("Enter Sheet Name")

End Sub
并确保用户未输入任何无效字符

您也可以这样做:

Sub tenloops1()
    Dim ws As Worksheet
    Dim sName as String
    sName = InputBox("Enter Sheet Name")

    ' also may want to check for sName being a valid sheet name here
    If Len(sName) > 0 Then 
      Set ws = Worksheets.Add()
      ws.Name = sName
    Else
      ' user clicked cancel
    End If
End Sub
并确保用户未输入任何无效字符

您也可以这样做:

Sub tenloops1()
    Dim ws As Worksheet
    Dim sName as String
    sName = InputBox("Enter Sheet Name")

    ' also may want to check for sName being a valid sheet name here
    If Len(sName) > 0 Then 
      Set ws = Worksheets.Add()
      ws.Name = sName
    Else
      ' user clicked cancel
    End If
End Sub

我会做一些稍微不同的事情,以最小化添加和命名工作表时可能发生的错误

逻辑

你必须注意以下几点

  • 图纸名称有效。i、 e它不是空字符串或不超过31个字符。两者都不应包含字符
    /,\,[,],*,?,:
  • 不应该已经有具有该名称的工作表
  • 错误处理
    关于错误转到…
    以捕获可能出现的任何其他错误
  • 代码

    Option Explicit
    
    Sub Sample()
        Dim Ret As Variant
        
        On Error GoTo Whoa
        
        '~~> Get user input
        Ret = InputBox("Enter a valid sheet name")
        
        If Ret = "" Then Exit Sub
        
        '~~> Check if the sheet name is valid
        If IsValidSheetName(Ret) = False Then
            MsgBox "The sheet name cannot have length more than 31 " & _
                   "characters. Neither it can contain the characters /,\,[,],*,?,:"
            Exit Sub
        End If
        
        '~~> Check if there is no other sheet with that name
        If DoesSheetExist(Ret) Then
            MsgBox "There is already a sheet with that name. Enter a new name"
            Exit Sub
        End If
        
        '~~> Add the sheet and name it in one go
        ThisWorkbook.Sheets.Add.Name = Ret
        
        Exit Sub
    Whoa:
        MsgBox Err.Description
    End Sub
    
    Private Function IsValidSheetName(userinput As Variant) As Boolean
        Dim IllegalChars As Variant
        Dim i As Long
        
        IllegalChars = Array("/", "\", "[", "]", "*", "?", ":")
        
        If Len(userinput) > 31 Then Exit Function
        
        For i = LBound(IllegalChars) To UBound(IllegalChars)
            If InStr(userinput, (IllegalChars(i))) > 0 Then Exit Function
        Next i
    
        IsValidSheetName = True
    End Function
    
    Private Function DoesSheetExist(userinput As Variant) As Boolean
        Dim wsh As Worksheet
        
        On Error Resume Next
        Set wsh = ThisWorkbook.Sheets(userinput)
        On Error GoTo 0
        
        If Not wsh Is Nothing Then DoesSheetExist = True
    End Function
    

    我会做一些稍微不同的事情,以最小化添加和命名工作表时可能发生的错误

    逻辑

    你必须注意以下几点

  • 图纸名称有效。i、 e它不是空字符串或不超过31个字符。两者都不应包含字符
    /,\,[,],*,?,:
  • 不应该已经有具有该名称的工作表
  • 错误处理
    关于错误转到…
    以捕获可能出现的任何其他错误
  • 代码

    Option Explicit
    
    Sub Sample()
        Dim Ret As Variant
        
        On Error GoTo Whoa
        
        '~~> Get user input
        Ret = InputBox("Enter a valid sheet name")
        
        If Ret = "" Then Exit Sub
        
        '~~> Check if the sheet name is valid
        If IsValidSheetName(Ret) = False Then
            MsgBox "The sheet name cannot have length more than 31 " & _
                   "characters. Neither it can contain the characters /,\,[,],*,?,:"
            Exit Sub
        End If
        
        '~~> Check if there is no other sheet with that name
        If DoesSheetExist(Ret) Then
            MsgBox "There is already a sheet with that name. Enter a new name"
            Exit Sub
        End If
        
        '~~> Add the sheet and name it in one go
        ThisWorkbook.Sheets.Add.Name = Ret
        
        Exit Sub
    Whoa:
        MsgBox Err.Description
    End Sub
    
    Private Function IsValidSheetName(userinput As Variant) As Boolean
        Dim IllegalChars As Variant
        Dim i As Long
        
        IllegalChars = Array("/", "\", "[", "]", "*", "?", ":")
        
        If Len(userinput) > 31 Then Exit Function
        
        For i = LBound(IllegalChars) To UBound(IllegalChars)
            If InStr(userinput, (IllegalChars(i))) > 0 Then Exit Function
        Next i
    
        IsValidSheetName = True
    End Function
    
    Private Function DoesSheetExist(userinput As Variant) As Boolean
        Dim wsh As Worksheet
        
        On Error Resume Next
        Set wsh = ThisWorkbook.Sheets(userinput)
        On Error GoTo 0
        
        If Not wsh Is Nothing Then DoesSheetExist = True
    End Function
    

    结构化示例调用

    • [1]
      获取用户输入
    • [2]
      如果图纸名称无效或已经存在,则在循环中重复该操作
    • [3]
      添加工作表并命名
    这种方法并不假装是最好的,但您可以从研究中获益,因为它在帮助函数
    b)
    c)

    函数
    SheetError()

    控制ExampleCall中工作表名称的用户输入,并使用两个帮助功能
    b)
    c)


    帮助功能b)
    SheetExists()

    允许单行检查:

    Private Function SheetExists(Sheetname As String, wb As Workbook) As Boolean
    'Purp.: check if sheet exists
    'Date:  2021-03-08
    'Auth.: https://stackoverflow.com/users/6460297/t-m
        SheetExists = Not IsError(Application.Evaluate("'" & wb.Path & "\[" & wb.Name & "]" & Sheetname & "'!A1"))
    End Function
    
    
    帮助函数c)
    IsValidSheetName()

    通过
    Application.Match()
    将从sheetname字符派生的字节数组(
    by
    与非法字符数组(
    illegalAsc
    )进行比较

    注意

    • Match()不限于1个数组参数! (显示非法发生、非发现错误的1基位置)
    • Count()忽略错误元素,因此它足以检测元素的至少一次出现
    工作表名称中不允许的非法字符是
    /\[]*?:

    Private Function IsValidSheetName(Sheetname As String) As Boolean
    'Auth.: https://stackoverflow.com/users/6460297/t-m
    'Purp.: check for valid sheet name
    'Date:   2021-03-08
        'a) length cannot exceed 31 characters
        If Len(Sheetname) > 31 Then Exit Function
        'b) define illegal character codes
        Dim IllegalAsc As Variant
        IllegalAsc = Array(47, 92, 91, 93, 42, 63, 58)  ' i.e. /\[]*?:
        'c) convert name to byte array
        Dim by() As Byte: by = Sheetname
        'd) return true if no counted occurrencies of illegal matches
        With Application
            IsValidSheetName = .Count(.Match(IllegalAsc, by, 0)) = 0   ' edited due to comment
        End With
    End Function
    

    结构化示例调用

    • [1]
      获取用户输入
    • [2]
      如果图纸名称无效或已经存在,则在循环中重复该操作
    • [3]
      添加工作表并命名
    这种方法并不假装是最好的,但您可以从研究中获益,因为它在帮助函数
    b)
    c)

    函数
    SheetError()

    控制ExampleCall中工作表名称的用户输入,并使用两个帮助功能
    b)
    c)


    帮助功能b)
    SheetExists()

    允许单行检查:

    Private Function SheetExists(Sheetname As String, wb As Workbook) As Boolean
    'Purp.: check if sheet exists
    'Date:  2021-03-08
    'Auth.: https://stackoverflow.com/users/6460297/t-m
        SheetExists = Not IsError(Application.Evaluate("'" & wb.Path & "\[" & wb.Name & "]" & Sheetname & "'!A1"))
    End Function
    
    
    帮助函数c)
    IsValidSheetName()

    通过
    Application.Match()
    将从sheetname字符派生的字节数组(
    by
    与非法字符数组(
    illegalAsc
    )进行比较

    注意

    • Match()不限于1个数组参数! (显示非法发生、非发现错误的1基位置)
    • Count()忽略错误元素,因此它足以检测元素的至少一次出现
    工作表名称中不允许的非法字符是
    /\[]*?:

    Private Function IsValidSheetName(Sheetname As String) As Boolean
    'Auth.: https://stackoverflow.com/users/6460297/t-m
    'Purp.: check for valid sheet name
    'Date:   2021-03-08
        'a) length cannot exceed 31 characters
        If Len(Sheetname) > 31 Then Exit Function
        'b) define illegal character codes
        Dim IllegalAsc As Variant
        IllegalAsc = Array(47, 92, 91, 93, 42, 63, 58)  ' i.e. /\[]*?:
        'c) convert name to byte array
        Dim by() As Byte: by = Sheetname
        'd) return true if no counted occurrencies of illegal matches
        With Application
            IsValidSheetName = .Count(.Match(IllegalAsc, by, 0)) = 0   ' edited due to comment
        End With
    End Function
    

    你有问题吗?我的代码不起作用。我无法使用用户名user providesTry
    ActiveSheet.name=InputBox(“输入工作表名称”)重命名添加的工作表
    并确保用户没有输入任何无效字符。发布了一个迟到的答案,展示了有效性检查中的一些新的系统性方面:-)@sathishthirunaukukarasu您有问题吗?我的代码不起作用。我无法使用用户名user providesTry
    ActiveSheet.name=InputBox(“输入工作表名”)
    重命名添加的工作表,并确保用户未输入任何无效字符。发布了一个最新的答案,演示了有效性检查中的一些新方法:-)@sathishthirunaukkarasugreat code。谢谢。请使用左上角的绿色复选标记选择一个被接受的答案,这样问题就不会一直悬而未决。@Sathishthirunaukkarasu您得到了几个有效答案:请随意将首选答案标记为正确。答案旁边的绿色复选标记表示接受-cf。。。拿着那张桌子。标记答案将有助于其他开发人员避免进入这个问题,因为它已经解决,并将注意力集中在其他问题上。当然,你也有足够的声誉来支持好的答案。谢谢伟大的代码。谢谢。请使用左上角的绿色复选标记选择一个被接受的答案,这样问题就不会一直悬而未决。@Sathishthirunaukkarasu您得到了几个有效答案:请随意将首选答案标记为正确。答案旁边的绿色复选标记表示接受-cf。。。拿着那张桌子。标记答案将有助于其他开发人员避免进入这个问题,因为它已经解决,并将注意力集中在其他问题上。