Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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 为什么代码在我激活工作表时有效,而在我不激活时无效';T_Excel_Vba - Fatal编程技术网

Excel 为什么代码在我激活工作表时有效,而在我不激活时无效';T

Excel 为什么代码在我激活工作表时有效,而在我不激活时无效';T,excel,vba,Excel,Vba,如下所示,如果我添加ws.Activate,这段代码运行得很好,但如果我不添加,它就会陷入围绕ws.unprotect的无休止的循环中。为什么呢?有没有办法让这个代码运行而不必激活每个工作表 Sub passwordbreaker() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets If ws.Visible = True Then ws.Activate Dim

如下所示,如果我添加ws.Activate,这段代码运行得很好,但如果我不添加,它就会陷入围绕ws.unprotect的无休止的循环中。为什么呢?有没有办法让这个代码运行而不必激活每个工作表

Sub passwordbreaker()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Visible = True Then
ws.Activate

            Dim I As Integer, j As Integer, k As Integer
            Dim l As Integer, m As Integer, n As Integer
            Dim i1 As Integer, i2 As Integer, i3 As Integer
            Dim i4 As Integer, i5 As Integer, i6 As Integer
            On Error Resume Next
            For I = 65 To 66: For j = 65 To 66: For k = 65 To 66
            For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
            For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
            For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
            ws.Unprotect Chr(I) & Chr(j) & Chr(k) & _
                Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
                Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
            If ws.ProtectContents = False Then
                 GoTo nextsht
            End If
            Next: Next: Next: Next: Next: Next
            Next: Next: Next: Next: Next: Next
        End If
nextsht:
    Next ws
End Sub

您希望忽略代码中哪些错误的可能重复?