突出显示Excel中的更改
我会发送一份电子表格,更新信息,然后发送回我 我将验证和锁定单元格,以强制用户输入准确的信息。我使用VBA禁用剪切复制和粘贴功能的解决方法。此外,我还插入了一个VBA函数,以强制用户在宏中打开Excel文件 我正在尝试跟踪更改,以便在收到工作表时知道更新了哪些内容。当有人保存文档时,我会收到一个错误,它会随机将我完全锁定在文档之外 如何通过VBA而不是Excel的“共享/跟踪更改”选项突出显示更改 此工作簿:突出显示Excel中的更改,excel,vba,Excel,Vba,我会发送一份电子表格,更新信息,然后发送回我 我将验证和锁定单元格,以强制用户输入准确的信息。我使用VBA禁用剪切复制和粘贴功能的解决方法。此外,我还插入了一个VBA函数,以强制用户在宏中打开Excel文件 我正在尝试跟踪更改,以便在收到工作表时知道更新了哪些内容。当有人保存文档时,我会收到一个错误,它会随机将我完全锁定在文档之外 如何通过VBA而不是Excel的“共享/跟踪更改”选项突出显示更改 此工作簿: Option Explicit Const WelcomePage = "Macro
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
在模块中:
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
选项显式
子切换CutCopyAndPaste(允许为布尔值)
'激活/停用剪切、复制、粘贴和粘贴特殊菜单项
调用启用项(21,允许)'cut
调用启用项(19,允许)'复制
调用EnableMenuItem(22,允许)'粘贴
调用EnableMenuItem(755,允许)'pastespecial
'激活/停用拖放功能
Application.celldragandrop=允许
'激活/停用剪切、复制、粘贴和粘贴特殊快捷键
应用
选择案例允许
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}”
结束选择
以
端接头
子启用项(ctlId为整数,启用为布尔值)
'激活/停用特定菜单项
作为命令栏的Dim cBar
Dim cBarCtrl作为CommandBarControl
对于Application.CommandBars中的每个cBar
如果cBar.Name为“剪贴板”,则
设置cBarCtrl=cBar.FindControl(ID:=ctlId,recursive:=True)
如果不是cBarCtrl,则cBarCtrl.Enabled=已启用
如果结束
下一个
端接头
子CutCopyPasteDisabled()
'通知用户功能已被禁用
MsgBox“此工作簿中已禁用剪切、复制和粘贴。请硬键输入数据。”
端接头
你为什么不去Ozgrid.com看看呢:
您可以直接轻松地实现代码,还可以添加一些功能,如以彩色突出显示更改的单元格等。为什么不查看Ozgrid.com:
您可以直接轻松地实现代码,还可以添加一些功能,如以彩色突出显示已更改的单元格等。我对您的模块进行了轻微修改,如下所示,并在“此工作簿”的“工作簿打开”和“工作簿关闭前”部分调用了该函数。在前者中,函数参数为False,而在后者中,参数为True。它工作得很好。您还可以参考Yogesh的代码,它更全面。其URL为: 将以下内容插入模块:
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
我稍微修改了您的模块,如下所示,并在“此工作簿”的“工作簿\u打开”和“工作簿\u之前关闭”部分调用了该函数。在前者中,函数参数为False,而在后者中,参数为True。它工作得很好。您还可以参考Yogesh的代码,它更全面。其URL为: 将以下内容插入模块:
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
当您需要跟踪和比较更改时,有一种根本不使用宏的简单方法:尝试 您可以将原始电子表格与从其他用户处收到的版本进行比较。理想情况下,他们还应该安装外接程序,但不一定
如果您想跟踪宏模块中的更改,那么这是一个救命稻草。当您需要跟踪和比较更改时,有一种根本不使用宏的简单方法:尝试 您可以将原始电子表格与从其他用户处收到的版本进行比较。理想情况下,他们还应该安装外接程序,但不一定 如果您想跟踪宏模块中的更改,那么这是一个救命稻草