May 24 2023 04:29 PM
May 24 2023 04:29 PM
Hi, I hope someone can help
I have an Excel Table used by multiple users. The Table has a number of conditional formatting rules that I need to maintain. Users sometimes paste in text from other sources, which messes up the conditional formatting rules. If I protect the sheet, the Table doesn’t automatically expand.
Is there a way to only allow paste as ‘paste special values’ (and ‘paste special as unformatted text when pasting text from Word)? Or a way that forces all pastes to be a paste as values/unformatted text? Ideally, a non-VBA solution, but happy to use VBA if not possible.
many thanks in advance
May 24 2023 11:11 PM
In Excel, there is no built-in option to restrict paste operations to "paste special values" or "paste special unformatted text" without using VBA.
However, you can achieve this functionality using VBA code.
Here is an example of how you can accomplish this:
Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next Application.EnableEvents = False ' Check if the pasted content contains formulas If Application.CutCopyMode = xlCopy Then If Not Intersect(Target, Me.UsedRange) Is Nothing Then If Target.HasFormula Then Target.PasteSpecial xlPasteValues End If End If End If ' Check if the pasted content is from Word If Application.CutCopyMode = xlCopy Then If Not Intersect(Target, Me.UsedRange) Is Nothing Then If Application.ClipboardFormats(1) = 2 Then Target.PasteSpecial xlPasteValues End If End If End If Application.EnableEvents = True End Sub
5. Save the workbook and close the VBA editor.
With this code in place, whenever a user pastes something into the protected worksheet, the code checks if the content has formulas or if it is copied from Word. If either condition is met, the pasted content is converted to values.
Please note that this VBA solution overrides the default paste behavior, so users will not be able to paste anything other than values or unformatted text. It is recommended to inform your users about this restriction to avoid confusion.
Remember to save your Excel file as a macro-enabled workbook (.xlsm) if it contains VBA code.
May 25 2023 08:28 AM
thank you so much @NikolinoDE for the step by step instructions and code; much appreciated.
just one clarification (& perhaps I should have been more precise); I want anything pasted in )from the clipboard) to be entered as ‘values’ - the source could be Excel, Word, web etc. (the only reason I mentioned Word, was that I noticed when pasting into excel any text copied from Word, the paste special option is ‘unformatted text’, rather than ‘as values’ when copied from excel).
would the code you provided work for anything being pasted in from the clipboard, regarding of source?
many thanks again
May 25 2023 08:31 AM