VBA宏检查工作表名称是否存在

标签 vba excel

我当前正在运行一个宏,该宏复制模板工作表、获取用户输入、重命名工作表并将用户输入放入工作表中。

除了检查工作表名称是否已存在的功能之外,一切似乎都正常。实际的“工作表已存在”有效,但是,在提示我此错误之前,它由于某种原因重复了模板工作表。

这是实际宏的代码(受影响的区域是:Sheets("TEMPLATE").Copy After:=Sheets("TEMPLATE"))

'============================================================================
'Button to load new GSA Form
'============================================================================
Private Sub CommandButton1_Click()

Const cstrTitle As String = "Create a new GSA worksheet"
Const cstrPrompt As String = "Enter the GSA number for the new worksheet"
Dim projName As String
Dim projAddress As String
Dim projDate As Date
Dim strInput As Variant                                     'Input value from user (GSA Number)
Dim strDefault As String: strDefault = ""                   'Sets default value for inputbox
Dim strInputErrorMessage As String                          'Error message
Dim booValidateOK As Boolean: booValidateOK = False
On Error GoTo HandleError

Do
    strInput = ActiveSheet.Range("C9").Value
    projName = ActiveSheet.Range("C6").Value
    projAddress = ActiveSheet.Range("C7").Value
    projDate = ActiveSheet.Range("C8").Value

    If Len(strInput) = 0 Then GoTo HandleExit
    GoSub ValidateInput
    If Not booValidateOK Then
        If vbCancel = MsgBox(strInputErrorMessage & "Retry?", vbExclamation + vbOKCancel) Then GoTo HandleExit
    End If
Loop While Not booValidateOK

Sheets("TEMPLATE").Copy After:=Sheets("TEMPLATE")                          'Copy Template Sheet, places the copy after the template sheet
ActiveSheet.Name = strInput                                                 'Renames the new sheet to the user's input

ActiveSheet.Range("C5").Value = projName                                    'Inputs Project Name to new sheet
ActiveSheet.Range("C6").Value = projAddress                                 'Inputs Project Address to new sheet
ActiveSheet.Range("C7").Value = projDate                                    'Inputs Project Date to new sheet
ActiveSheet.Range("C8").Value = strInput                                    'Inputs GSA # to new sheet

ThisWorkbook.Worksheets("MASTER").Range("C6").Value = ""   'name'           'Deletes inputs from MASTER sheet
ThisWorkbook.Worksheets("MASTER").Range("C7").Value = ""   'address'
ThisWorkbook.Worksheets("MASTER").Range("C8").Value = ""   'date'
ThisWorkbook.Worksheets("MASTER").Range("C9").Value = ""   'gsa #'



HandleExit:
Exit Sub
HandleError:
MsgBox Err.Description
Resume HandleExit

ValidateInput:
If SheetExists(strSheetName:=strInput) Then
    strInputErrorMessage = "Sheet already exists. "
Else
    booValidateOK = True
End If
Return
End Sub

这是检查工作表是否已存在的函数

Public Function SheetExists(strSheetName As Variant, Optional wbWorkbook As Workbook) As Boolean
If wbWorkbook Is Nothing Then Set wbWorkbook = ActiveWorkbook           'or ThisWorkbook - whichever appropriate
Dim obj As Object
On Error GoTo HandleError
Set obj = wbWorkbook.Sheets(strSheetName)
SheetExists = True
Exit Function
HandleError:
 SheetExists = False
End Function

最佳答案

试试这个:

Function sheetExists(sheetToFind As String) As Boolean
    sheetExists = False
    For Each sheet In Worksheets
        If sheetToFind = sheet.name Then
            sheetExists = True
            Exit Function
        End If
    Next sheet
End Function

并像这样使用:

if sheetExists("TEMPLATE") = true then
  'your code
else
  'code
end if

Excel VBA If WorkSheet("wsName") Exists

关于VBA宏检查工作表名称是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51049967/

相关文章:

excel - 保护 Excel VBA 代码的最佳方法?

vba - 可以使用 IDTExtensibility2 为 VBA IDE 创建 64 位插件吗?

ms-access - MS Access 查找向导列显示 ID 而不是名称

Python--读取 dat 文件行,重写 Excel 中的列。 csv/numpy/openpyxl

excel - 12 个字符,包括前导零和后跟零

vba - 访问工作表上绘制的标签

excel - 如何检查 Excel 表格单元格是否已被用户编辑?

vba - 以编程方式从 excel 文件中提取 excel vba 宏

excel - VLookUp 函数 VBA - 未返回所需值

excel - 在 VBA excel 中的 VLOOKUP 中设置 table_array