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
我会做一些稍微不同的事情,以最小化添加和命名工作表时可能发生的错误 逻辑 你必须注意以下几点
/,\,[,],*,?,:
关于错误转到…
以捕获可能出现的任何其他错误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
我会做一些稍微不同的事情,以最小化添加和命名工作表时可能发生的错误 逻辑 你必须注意以下几点
/,\,[,],*,?,:
关于错误转到…
以捕获可能出现的任何其他错误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 providesTryActiveSheet.name=InputBox(“输入工作表名”)
重命名添加的工作表,并确保用户未输入任何无效字符。发布了一个最新的答案,演示了有效性检查中的一些新方法:-)@sathishthirunaukkarasugreat code。谢谢。请使用左上角的绿色复选标记选择一个被接受的答案,这样问题就不会一直悬而未决。@Sathishthirunaukkarasu您得到了几个有效答案:请随意将首选答案标记为正确。答案旁边的绿色复选标记表示接受-cf。。。拿着那张桌子。标记答案将有助于其他开发人员避免进入这个问题,因为它已经解决,并将注意力集中在其他问题上。当然,你也有足够的声誉来支持好的答案。谢谢伟大的代码。谢谢。请使用左上角的绿色复选标记选择一个被接受的答案,这样问题就不会一直悬而未决。@Sathishthirunaukkarasu您得到了几个有效答案:请随意将首选答案标记为正确。答案旁边的绿色复选标记表示接受-cf。。。拿着那张桌子。标记答案将有助于其他开发人员避免进入这个问题,因为它已经解决,并将注意力集中在其他问题上。