2009-08-28 6 views
1

J'ai une feuille de calcul que j'envoie à divers endroits pour avoir des informations à ce sujet mises à jour, puis renvoyé à moi. Cependant, j'ai dû mettre la validation et verrouiller les cellules pour forcer les utilisateurs à entrer des informations précises. Ensuite, je peux utiliser VBA pour désactiver le travail des fonctions couper et coller. Et en plus, j'ai inséré une fonction VBA pour forcer les utilisateurs à ouvrir le fichier Excel dans les macros. Maintenant, j'essaie de suivre les changements afin que je sache ce qui a été mis à jour lorsque je reçois la feuille. Cependant chaque fois que je fais ceci je reçois une erreur quand quelqu'un sauve le document et aléatoirement il me verrouillera complètement hors du document.Utilisation de VBA/Macro pour mettre en surbrillance les changements dans Excel

J'ai mon code collé ci-dessous, quelqu'un peut-il m'aider à créer du code dans le forum VBA pour mettre en évidence les changements au lieu d'utiliser l'option de changement de piste/partage d'Excel?

ThisWorkbook (Code):

Option Explicit 

Const WelcomePage = "Macros" 

Private Sub Workbook_BeforeClose(Cancel As Boolean) 
    Call ToggleCutCopyAndPaste(True) 

    'Turn off events to prevent unwanted loops 
    Application.EnableEvents = False 

    'Evaluate if workbook is saved and emulate default propmts 
    With ThisWorkbook 
     If Not .Saved Then 
      Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _ 
       vbYesNoCancel + vbExclamation) 
      Case Is = vbYes 
       'Call customized save routine 
       Call CustomSave 
      Case Is = vbNo 
       'Do not save 
      Case Is = vbCancel 
       'Set up procedure to cancel close 
       Cancel = True 
      End Select 
     End If 

     'If Cancel was clicked, turn events back on and cancel close, 
     'otherwise close the workbook without saving further changes 
     If Not Cancel = True Then 
      .Saved = True 
      Application.EnableEvents = True 
      .Close savechanges:=False 
     Else 
      Application.EnableEvents = True 
     End If 
    End With 


End Sub 

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 
    'Turn off events to prevent unwanted loops 
    Application.EnableEvents = False 

    'Call customized save routine and set workbook's saved property to true 
    '(To cancel regular saving) 
    Call CustomSave(SaveAsUI) 
    Cancel = True 

    'Turn events back on an set saved property to true 
    Application.EnableEvents = True 
    ThisWorkbook.Saved = True 
End Sub 

Private Sub Workbook_Open() 
    Call ToggleCutCopyAndPaste(False) 

    'Unhide all worksheets 
    Application.ScreenUpdating = False 
    Call ShowAllSheets 
    Application.ScreenUpdating = True 
End Sub 

Private Sub CustomSave(Optional SaveAs As Boolean) 
    Dim ws As Worksheet, aWs As Worksheet, newFname As String 
    'Turn off screen flashing 
    Application.ScreenUpdating = False 

    'Record active worksheet 
    Set aWs = ActiveSheet 

    'Hide all sheets 
    Call HideAllSheets 

    'Save workbook directly or prompt for saveas filename 
    If SaveAs = True Then 
     newFname = Application.GetSaveAsFilename(_ 
     fileFilter:="Excel Files (*.xls), *.xls") 
     If Not newFname = "False" Then ThisWorkbook.SaveAs newFname 
    Else 
     ThisWorkbook.Save 
    End If 

    'Restore file to where user was 
    Call ShowAllSheets 
    aWs.Activate 

    'Restore screen updates 
    Application.ScreenUpdating = True 
End Sub 

Private Sub HideAllSheets() 
    'Hide all worksheets except the macro welcome page 
    Dim ws As Worksheet 

    Worksheets(WelcomePage).Visible = xlSheetVisible 

    For Each ws In ThisWorkbook.Worksheets 
     If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden 
    Next ws 

    Worksheets(WelcomePage).Activate 
End Sub 

Private Sub ShowAllSheets() 
    'Show all worksheets except the macro welcome page 

    Dim ws As Worksheet 

    For Each ws In ThisWorkbook.Worksheets 
     If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible 
    Next ws 

    Worksheets(WelcomePage).Visible = xlSheetVeryHidden 

End Sub 

Private Sub Workbook_Activate() 
    Call ToggleCutCopyAndPaste(False) 
End Sub 


Private Sub Workbook_Deactivate() 
    Call ToggleCutCopyAndPaste(True) 
End Sub 




This is in my ModuleCode: 


Option Explicit 

Sub ToggleCutCopyAndPaste(Allow As Boolean) 
    'Activate/deactivate cut, copy, paste and pastespecial menu items 
    Call EnableMenuItem(21, Allow) ' cut 
    Call EnableMenuItem(19, Allow) ' copy 
    Call EnableMenuItem(22, Allow) ' paste 
    Call EnableMenuItem(755, Allow) ' pastespecial 

    'Activate/deactivate drag and drop ability 
    Application.CellDragAndDrop = Allow 

    'Activate/deactivate cut, copy, paste and pastespecial shortcut keys 
    With Application 
     Select Case Allow 
     Case Is = False 
      .OnKey "^c", "CutCopyPasteDisabled" 
      .OnKey "^v", "CutCopyPasteDisabled" 
      .OnKey "^x", "CutCopyPasteDisabled" 
      .OnKey "+{DEL}", "CutCopyPasteDisabled" 
      .OnKey "^{INSERT}", "CutCopyPasteDisabled" 
     Case Is = True 
      .OnKey "^c" 
      .OnKey "^v" 
      .OnKey "^x" 
      .OnKey "+{DEL}" 
      .OnKey "^{INSERT}" 
     End Select 
    End With 
End Sub 

Sub EnableMenuItem(ctlId As Integer, Enabled As Boolean) 
    'Activate/Deactivate specific menu item 
    Dim cBar As CommandBar 
    Dim cBarCtrl As CommandBarControl 
    For Each cBar In Application.CommandBars 
     If cBar.Name <> "Clipboard" Then 
      Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True) 
      If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Enabled 
     End If 
    Next 
End Sub 

Sub CutCopyPasteDisabled() 
    'Inform user that the functions have been disabled 
    MsgBox " Cutting, copying and pasting have been disabled in this workbook. Please hard key in data. " 
End Sub 

Répondre

0

Pourquoi ne pas vérifier Ozgrid.com:

http://www.ozgrid.com/VBA/track-changes.htm

Vous pouvez directement mettre en œuvre le code facilement et ajouter plusieurs fonctionnalités telles que mettre en évidence les cellules changées, etc. en couleur.

0

J'ai légèrement modifié votre module comme indiqué ci-dessous et appelé la fonction dans les sections 'Workbook_Open' et 'Workbook_Beforeclose' de 'This Workbook'. Dans le premier, l'argument de la fonction était Faux, alors que dans l'autre, l'argument était Vrai. Ça marche bien. Vous feriez aussi bien de vous référer au code de Yogesh, qui est plus complet. L'URL qui est: http://ygblogs.blogspot.com/2009/04/macros-in-excel-disable-cut-copy-paste.html

Insérer le texte suivant dans un module:

Option Explicit 
Dim Allow As Boolean, ctlId As Integer, Enabled As Boolean 

Function ToggleCutCopyAndPaste(Allow As Boolean) 

    'Activate/deactivate cut, copy, paste and pastespecial shortcut keys 
    With Application 
     Select Case Allow 
      Case False 
       .OnKey "^c", "CutCopyPasteDisabled" 
       .OnKey "^v", "CutCopyPasteDisabled" 
       .OnKey "^x", "CutCopyPasteDisabled" 
       .OnKey "+{DEL}", "CutCopyPasteDisabled" 
       .OnKey "^{INSERT}", "CutCopyPasteDisabled"   
      Case True 
       .OnKey "^c" 
       .OnKey "^v" 
       .OnKey "^x" 
       .OnKey "+{DEL}" 
       .OnKey "^{INSERT}" 
     End Select 
     .CutCopyMode = Allow 
     .CellDragAndDrop = Allow 
    End With 

    'Activate/Deactivate specific menu item 
    Dim cBar As CommandBar 
    Dim cBarCtrl As CommandBarControl, i As Integer 

    For i = 1 To 4 
     If i = 1 Then ctlId = 21 
     If i = 2 Then ctlId = 19 
     If i = 3 Then ctlId = 22 
     If i = 4 Then ctlId = 755   

     For Each cBar In Application.CommandBars 
      If cBar.Name <> "Clipboard" Then 
       Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True) 
       If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Allow 
      End If 
     Next 
    Next i  
End Function 

Insérer le texte suivant dans la section ThisWorkbook de l'éditeur VBA:

Private Sub Workbook_BeforeClose(Cancel As Boolean)  
    ToggleCutCopyAndPaste (True) 
End Sub 

Private Sub Workbook_Open()  
    ToggleCutCopyAndPaste (False)  
End Sub 
0

Lorsque vous devez suivre et comparez les changements, il y a un moyen facile sans macros du tout: essayez le Version Control add-in for Excel.

Vous pouvez comparer votre feuille de calcul d'origine avec les versions reçues d'autres utilisateurs. Idéalement, ils devraient également avoir le complément installé, mais pas nécessairement.

Si vous voulez garder la trace des changements dans vos modules de macro, alors ce Version Control for VBA macros est une bouée de sauvetage.

Questions connexes