VBA Code Interferring with User's Copy Action
OK. Here's something for you to file under "Kludge"
Don't laugh yet. Here's what you can do: On activate, check CutCopyMode. If
not False, add a new worksheet and paste. Turn off your menu, then copy the
current selection (what just got pasted) back to the clipboard, hide the new
sheet, and switch back to the sheet that was originally active. User can now
paste freely!
To avoid adding a new worksheet each time the user copies something in, use
a global variable called HiddenSheet, and only add the new sheet if
HiddenSheet is Nothing.
To avoid keeping the hidden sheet around forever, delete it on the
BeforeSave event.
Here's the code:
Dim HiddenSheet As Worksheet
Private Sub Workbook_Activate()
Dim CurrentSheet As Worksheet
If Application.CutCopyMode < False Then
Set CurrentSheet = ActiveSheet
If HiddenSheet Is Nothing Then
Worksheets.Add
Set HiddenSheet = ActiveSheet
Else
HiddenSheet.Visible = xlSheetVisible
HiddenSheet.Activate
End If
HiddenSheet.Paste
Application.DisplayFormulaBar = False
Selection.Copy
HiddenSheet.Visible = xlSheetHidden
CurrentSheet.Activate
Else
Application.DisplayFormulaBar = False
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
If Not HiddenSheet Is Nothing Then
Application.DisplayAlerts = False
HiddenSheet.Delete
Set HiddenSheet = Nothing
End If
End Sub
|