【问题标题】:Unable to set the text property of the characters class无法设置字符类的文本属性
【发布时间】:2020-03-19 15:01:29
【问题描述】:

此代码保护除一张纸之外的所有纸。如果工作表受到保护,则此方法有效,但当它们不受保护时会给我一条错误消息。

ws 是按钮所在的工作表。

wsheet 用于保护除一张纸之外的所有纸。本质上,当用户单击工作表 ws 的按钮时,我希望所有工作表(包括按钮所在的工作表)都受到保护,但名为“加班”的工作表除外。当他们再次单击它时,它会取消对所有工作表的保护。

Dim wSheet As Worksheet
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Sheets(1)


        Application.ScreenUpdating = False

            For Each wSheet In Worksheets
                If wSheet.Name = "Overtime" Then
                    wSheet.Unprotect Password:="12345"
                    ws.Shapes("Rectangle_LOCK").TextFrame.Characters.Text = "Vérouiller" 'THIS IS WHERE IT GIVES ME THE ERROR
                ElseIf wSheet.ProtectContents = True Then
                    wSheet.Unprotect Password:="12345"
                    ws.Shapes("Rectangle_LOCK").TextFrame.Characters.Text = "Vérouiller"
                Else

                    wSheet.Unprotect Password:="12345"
                    ws.Shapes("Rectangle_LOCK").TextFrame.Characters.Text = "Déverouiller"
                    wSheet.Protect Password:="12345"


                End If
            Next wSheet

 Application.ScreenUpdating = True

【问题讨论】:

  • 它在该行上给您一个错误,因为它上面的行正在保护工作表。工作表需要不受保护。
  • 尝试先添加一行取消保护...仍然给我错误
  • 看起来 wSheetws 在循环中的特定迭代中没有引用同一个工作表。我建议你编辑你的帖子并准确地描述你想要做什么。
  • 添加了更多细节

标签: excel vba


【解决方案1】:

试试下面的代码...

Dim wSheet As Worksheet
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets(1)


Application.ScreenUpdating = False

For Each wSheet In wb.Worksheets
    If wSheet.Name <> "Overtime" Then
        If wSheet.ProtectContents Then
            wSheet.Unprotect Password:="12345"
            If wSheet.Name = ws.Name Then
                wSheet.Shapes("Rectangle_LOCK").TextFrame.Characters.Text = "Vérouiller"
            End If
        Else
            If wSheet.Name = ws.Name Then
                wSheet.Shapes("Rectangle_LOCK").TextFrame.Characters.Text = "Déverouiller"
            End If
            wSheet.Protect Password:="12345"
        End If
    End If
Next wSheet

Application.ScreenUpdating = True

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多