2016-07-16 4 views
0

Ich habe Probleme mit meinem Makro, es läuft reibungslos vom Desktop aber wenn ich die gleiche Datei im Netzlaufwerk, andere Benutzer konfrontiert Runtime error 91 Object variable or With block variable not set.
Lassen Sie mich auch wissen, wie geschützte Ansicht mit Makro entfernen.VBA Laufzeitfehler '91': Objektvariable oder mit Blockvariable nicht gesetzt

Ihre Hilfe für folgenden Code benötigen:

In Modul

Sub UnhideAllSheets() 
    'Unhide all sheets in workbook. 
    Dim ws As Worksheet 
    For Each ws In ActiveWorkbook.Worksheets 
     ws.Visible = xlSheetVisible 
    Next ws 
End Sub 
Sub HideAllSheets() 
    'Unhide all sheets in workbook. 
    Call UnhideAllSheets 
    Dim ws As Worksheet 
    For Each ws In ThisWorkbook.Worksheets 
     If InStr(1, ws.Name, "START", vbTextCompare) = 0 _ 
      And InStr(1, ws.Name, "Data", vbTextCompare) = 0 Then 
      ws.Visible = xlVeryHidden 
     End If 
    Next ws 
End Sub 


Sub DRD() 
    Dim ws As Worksheet 

    Call HideAllSheets 
    'If VBA.Environ("Username") = "140736TJ" Then 
    'Sheets("8.Generator").Visible = xlSheetVisible 
    'Sheets("8.Generator").Select 

    With ActiveWorkbook.Worksheets 
     Select Case VBA.Environ("Username") 
      Case "130349TJ", "130355TJ" 
       Sheets("1.Large MCH").Visible = xlSheetVisible 
       Sheets("1.Large MCH").Select 
      Case "140646TJ" 
       Sheets("2.Large FAB").Visible = xlSheetVisible 
       Sheets("2.Large FAB").Select 
      Case "130361TJ" 
       Sheets("3.Blade").Visible = xlSheetVisible 
       Sheets("3.Blade").Select 
      Case "120243TJ" 
       Sheets("4.Nozzle").Visible = xlSheetVisible 
       Sheets("4.Nozzle").Select 
      Case "130360TJ", "146061tc" 
       Sheets("5.T.Assy").Visible = xlSheetVisible 
       Sheets("5.T.Assy").Select 
      Case "110206TJ" 
       Sheets("6.Rotor").Visible = xlSheetVisible 
       Sheets("6.Rotor").Select 
      Case "120237TJ" 
       Sheets("7. Control Valve").Visible = xlSheetVisible 
       Sheets("7. Control Valve").Select 
      Case "140736TJ", "110088TJ", "130344TJ" 
       Call UnhideAllSheets 
       Sheets("DRD Index Consolidation").Select 
      Case "120234TJ" 
       Call UnhideAllSheets 
       Sheets("DRD Index Consolidation").Select 
       Call StopDeleteRowCols 
      Case Else 
       MsgBox "ACCESS DENIED" 
       ActiveWorkbook.Close 
       Call ResetDeleteRowCols 
     End Select 
    End With 

End Sub 


Sub StopDeleteRowCols() 
    Dim ctl As CommandBarControl 
    For Each ctl In Application.CommandBars.FindControls(ID:=293) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=294) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=296) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=3181) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=292) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=3125) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=21) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=945) 
     ctl.Enabled = False 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=4) 
     ctl.Enabled = False 
    Next ctl 

End Sub 


Sub ResetDeleteRowCols() 
    Dim ctl As CommandBarControl 
    For Each ctl In Application.CommandBars.FindControls(ID:=293) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=294) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=296) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=3181) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=292) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=3125) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=21) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=945) 
     ctl.Enabled = True 
    Next ctl 
    For Each ctl In Application.CommandBars.FindControls(ID:=4) 
     ctl.Enabled = True 
    Next ctl 

End Sub 


Private Sub Workbook_Open() 
    DRD 

End Sub 
+2

Welche Zeile gibt den Fehler? –

Antwort

0

Sie sollten sagen, welche Zeile Sie den Fehler bekommen. Ich bin mir sicher, der Fehler kommt von Application.CommandBars.FindControlsID:=?) keine Steuerelemente zu finden. Um den Fehler aufzufangen, erstelle ich eine Methode Sub BatchEnableControls(Enable As Boolean), um die Steuerelemente zu finden und zu aktivieren/deaktivieren. Ich habe auch einige andere Änderungen vorgenommen, die den Code leichter nachvollziehbar und erweiterbar machen.

Sub DRD() 
    Dim ws As Worksheet 

    Call HideAllSheets 
    'If VBA.Environ("Username") = "140736TJ" Then 
    'Sheets("8.Generator").Visible = xlSheetVisible 
    'Sheets("8.Generator").Select 


    Select Case VBA.Environ("Username") 
     Case "130349TJ" 
      Sheets("1.Large MCH").Visible = xlSheetVisible 
      Sheets("1.Large MCH").Select 
     Case "140646TJ" 
      Sheets("2.Large FAB").Visible = xlSheetVisible 
      Sheets("2.Large FAB").Select 
     Case "130361TJ" 
      Sheets("3.Blade").Visible = xlSheetVisible 
      Sheets("3.Blade").Select 
     Case "120243TJ" 
      Sheets("4.Nozzle").Visible = xlSheetVisible 
      Sheets("4.Nozzle").Select 
     Case "130360TJ" 
      Sheets("5.T.Assy").Visible = xlSheetVisible 
      Sheets("5.T.Assy").Select 
     Case "110206TJ" 
      Sheets("6.Rotor").Visible = xlSheetVisible 
      Sheets("6.Rotor").Select 
     Case "120237TJ" 
      Sheets("7. Control Valve").Visible = xlSheetVisible 
      Sheets("7. Control Valve").Select 
     Case "130355TJ" 
      Sheets("1.Large MCH").Visible = xlSheetVisible 
      Sheets("1.Large MCH").Select 
     Case "146061tc" 
      Sheets("5.T.Assy").Visible = xlSheetVisible 
      Sheets("5.T.Assy").Select 
     Case "140736TJ", "110088TJ", "130344TJ", "120234TJ" 
      Call UnhideAllSheets 
      Sheets("DRD Index Consolidation").Select 

     Case "120234TJ" 
      Call UnhideAllSheets 
      Sheets("DRD Index Consolidation").Select 
      BatchEnableControls False 

     Case Else 
      MsgBox "ACCESS DENIED" 

      BatchEnableControls True 

      ActiveWorkbook.Close 

    End Select 

End Sub 


Sub BatchEnableControls(Enable As Boolean) 

    EnableControls Enable, 293 
    EnableControls Enable, 294 
    EnableControls Enable, 296 
    EnableControls Enable, 3181 
    EnableControls Enable, 292 
    EnableControls Enable, 3125 
    EnableControls Enable, 21 
    EnableControls Enable, 945 
    EnableControls Enable, 4 

End Sub 

Sub EnableControls(Enable As Boolean, ControlID As Long) 
    Dim ctl As CommandBarControl 
    On Error Resume Next 
    For Each ctl In Application.CommandBars.FindControls(ID:=ControlID) 
     ctl.Enabled = Enable 
    Next ctl 

    If Err.Number <> 0 Then 
     Debug.Print "Error: " & Err.Description & " Could not find Control ID:=& ControlID " 
    End If 
    On Error GoTo zero 
End Sub 
+0

@ Thosmas Herr; Immer noch der gleiche Fehler. –

+0

In Excel-Option, wenn ich accept alle Makros standardmäßig aktivieren diese Laufzeitfehler 91 kommt, wenn ich Makro deaktivieren und manuell jedes Mal beim Öffnen des Blattes akzeptieren, dann ist alles in Ordnung. Bitte helfen Sie –

+0

Welche Zeile gibt Ihnen den Fehler? –