工作表代号错误

时间:2017-12-25 20:59:31

标签: excel vba excel-vba

当使用下面的代码通过代号引用工作表时,我会为“Sheet34”的每个实例打开文件资源管理器。我该如何防止这种情况?

Private Sub CheckBox4_Click()
Application.ScreenUpdating = False

Dim ws As Worksheet
Dim sheetName As String


If CheckBox4.Value = True Then
    ActiveWorkbook.Unprotect
    Worksheets("SUMMARY").Unprotect
    sheetName = Sheets("Control").Cells(16, "I")
    If sheetName = "" Then
        MsgBox "You must enter a valid Allowance descriptor. No entry was detected."
        CheckBox4.Value = False
        Exit Sub
    End If
    If Len(sheetName) > 31 Then
        MsgBox "Worksheet tab names cannot be greater than 31 characters in length."
        Application.EnableEvents = False
        Sheets("Control").Cells(16, "I").ClearContents
        Application.EnableEvents = True
        CheckBox4.Value = False
        Exit Sub
    End If
    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
        If InStr(sheetName, (IllegalCharacter(i))) > 0 Then
            MsgBox "You used a character that violates sheet naming rules. Please refrain from the following characters: / \ [ ] * ? : "
            Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If

    Next i
    If Sheets("Control").Range("I16") = Sheets("Control").Range("I17") Then
        MsgBox "There is already an Allowance with this name. Please choose a different name."
        Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If
        If Sheets("Control").Range("I16") = Sheets("Control").Range("I18") Then
        MsgBox "There is already an Allowance with this name. Please choose a different name."
        Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If
        If Sheets("Control").Range("I16") = Sheets("Control").Range("I21") Then
        MsgBox "There is already an Other Item with this name. Please choose a different name."
        Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If
        If Sheets("Control").Range("I16") = Sheets("Control").Range("I22") Then
        MsgBox "There is already an Other Item with this name. Please choose a different name."
        Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If
        If Sheets("Control").Range("I16") = Sheets("Control").Range("I23") Then
        MsgBox "There is already an Other Item with this name. Please choose a different name."
        Application.EnableEvents = False
            Sheets("Control").Cells(16, "I").ClearContents
            Application.EnableEvents = True
            CheckBox4.Value = False
            Exit Sub
        End If
    If WorksheetExists(sheetName) Then
        Worksheets(sheetName).Visible = -1
        Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False
        Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"
        Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"
        Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"
        Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"
        Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"
        Worksheets("SUMMARY").Cells(47, 6).Value = "=" & sheetName & "!$H$69"
        Worksheets("SUMMARY").Cells(47, 7).Value = "=" & sheetName & "!$J$69"
        Worksheets("SUMMARY").Cells(47, 8).Value = "=" & sheetName & "!$N$69"
        Worksheets("SUMMARY").Cells(47, 9).Value = "=" & sheetName & "!$P$69"
        Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"
        Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"
        Worksheets("SUMMARY").Cells(47, 12).Value = "=" & sheetName & "!$U$69"
        Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"
        ActiveWorkbook.Protect
        Sheets(sheetName).Protect
        Sheets("SUMMARY").Protect
        Exit Sub
    Else
        Set ws = ActiveWorkbook.Sheet34
        ws.Name = sheetName
        ws.Protect
        ws.EnableSelection = xlUnlockedCells
        Application.CutCopyMode = False

        Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False
        Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"
        Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"
        Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"
        Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"
        Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"
        Worksheets("SUMMARY").Cells(47, 6).Value = "=" & ws.Name & "!$H$69"
        Worksheets("SUMMARY").Cells(47, 7).Value = "=" & ws.Name & "!$J$69"
        Worksheets("SUMMARY").Cells(47, 8).Value = "=" & ws.Name & "!$N$69"
        Worksheets("SUMMARY").Cells(47, 9).Value = "=" & ws.Name & "!$P$69"
        Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"
        Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"
        Worksheets("SUMMARY").Cells(47, 12).Value = "=" & ws.Name & "!$U$69"
        Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"

        ActiveWorkbook.Protect
        Sheets(sheetName).Protect
        Sheets("SUMMARY").Protect
        Worksheets("Control").Activate

    End If

End If
If CheckBox4.Value = False Then
    ActiveWorkbook.Unprotect
    Worksheets("SUMMARY").Unprotect
    sheetName = Sheets("Control").Cells(16, "I")

    If WorksheetExists(sheetName) Then
        Worksheets(sheetName).Visible = 2
        Worksheets("SUMMARY").Rows("47").EntireRow.ClearContents
        Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = True
        ActiveWorkbook.Protect
        Sheets(sheetName).Protect
        Sheets("SUMMARY").Protect
    End If

   End If
    Application.ScreenUpdating = True
End Sub

跟进问题:此代码正在使用现有工作表并重命名。我担心的是,如果用户在单元格I16中命名工作表,然后重命名它,并取消选中或选中该框,我会在摘要工作表上找到一行,但引用不当。此外,代码不允许我输入下面的代码

Worksheets("SUMMARY").Cells(47, 6).Value = "=" & sheetName & "!$H$69"

作为

Worksheets("SUMMARY").Cells(47, 6).Value = "="' & sheetName & '"!$H$69"

因为它变成了& sheetName&发表评论。我该如何解决这个问题,以便在代码中正确引用工作表?我是否需要保留代号工作表?

我对如何停止重命名问题的想法是保护命名单元格,只要复选框值为正,并允许用户在复选框值为false时编辑单元格;可能会添加一行代码来检查单元格是否正在被更改,并询问用户是否确定这是他们想要修改的单元格,这将清除现有电子表格的数据,而不是格式化。

1 个答案:

答案 0 :(得分:0)

您正尝试将单元格的值设置为公式...请尝试以下操作:

  1. 创建指向同一工作簿中另一个工作表中的单元格的直接链接
  2. 打开另一个工作簿。
  3. 创建指向其他工作簿中工作表中单元格的直接链接
  4. 您会发现以下内容适用于工作表参考:

    Worksheets("SUMMARY").Cells(47, 6).Formula = "='" & sheetName & "'!$H$69"
    

    以下是工作簿参考:

    Worksheets("SUMMARY").Cells(47, 6).Formula = "='[" & Worboookpath & "] & sheetName & "'!$H$69"