SOLVED

Unable to lock cell formatting

%3CLINGO-SUB%20id%3D%22lingo-sub-1604686%22%20slang%3D%22en-US%22%3EUnable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1604686%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20a%20user%20pastes%20data%20onto%20a%20worksheet%2C%20how%20can%20I%20prevent%20the%20pasted%20data%20from%20erasing%20the%20formatting%20(cell%20borders%2C%20background%20color%2C%20number%20formats%2C%20etc.)%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20following%20method%20(which%20I%20found%20in%20several%20places%20on%20the%20web)%20does%20not%20work%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3ESelect%20all%20cells%20on%20the%20worksheet%3C%2FLI%3E%3CLI%3EHome%20tab%20-%26gt%3B%20Cells%20group%20-%26gt%3B%20Format%20-%26gt%3B%20Format%20Cells...%20-%26gt%3B%26nbsp%3Buncheck%20%22Locked%22%20-%26gt%3B%20OK%3C%2FLI%3E%3CLI%3EHome%20tab%20-%26gt%3B%20Cells%20group%20-%26gt%3B%20Format%20-%26gt%3B%20Protect%20Sheet%20-%26gt%3B%20make%20sure%20%22format%20cells%22%20is%20unchecked%22%20and%20do%20not%20enter%20a%20password%20-%26gt%3B%20OK%20(see%20screenshot%20below)%3C%2FLI%3E%3C%2FOL%3E%3CP%3EAfter%20doing%20this%2C%20the%20formatting%20groups%20in%20the%20Home%20tab%20are%20all%20grayed%20out%20(good).%26nbsp%3B%20However%2C%20when%20I%20paste%20into%20the%20sheet%2C%20the%20formatting%20in%20the%20pasted%20range%20is%20erased.%26nbsp%3B%20What%20am%20I%20doing%20wrong%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1604930%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1604930%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382386%22%20target%3D%22_blank%22%3E%40perkin_warbeck%3C%2FA%3E%26nbsp%3BI%20don't%20know%20that%20you%20can%20get%20around%20that.%26nbsp%3B%20You%20could%20do%20it%20using%20VBA.%26nbsp%3B%20I%20don't%20know%20what%20sort%20of%20formatting%20you%20have%20but%20here%20is%20a%20sample%20of%20what%20you%20might%20do%20in%20VBA%20(I%20assume%20you%20still%20lock%20the%20worksheet%20to%20make%20sure%20they%20only%20paste%20where%20you%20want%20them%20to%20paste).%26nbsp%3B%20The%20following%20VBA%20would%20be%20added%20to%20the%20specific%20sheet%20you%20want%20to%20control%20in%20VBA.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%20%20%20%20ActiveSheet.Unprotect%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20With%20Target%0A%20%20%20%20%20%20%20%20.ClearFormats%0A%20%20%20%20%20%20%20%20.Font.FontStyle%20%3D%20%22Times%20New%20Roman%22%0A%20%20%20%20%20%20%20%20.Font.Bold%20%3D%20True%0A%20%20%20%20%20%20%20%20.Interior.Color%20%3D%20RGB(250%2C%20250%2C%200)%0A%20%20%20%20%20%20%20%20.Locked%20%3D%20False%0A%20%20%20%20End%20With%0A%20%20%20%20Application.EnableEvents%20%3D%20True%0A%20%20%20%20ActiveSheet.Protect%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1605142%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1605142%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F570951%22%20target%3D%22_blank%22%3E%40mtarler%3C%2FA%3E%26nbsp%3BMy%20application%20is%20written%20entirely%20in%20VBA%2C%20and%20I%20am%20already%20handling%20Worksheet_Change%20events.%26nbsp%3B%20My%20understanding%20is%20that%20Worksheet_Change%20is%20called%20AFTER%20the%20change%20occurs.%26nbsp%3B%20In%20that%20case%2C%20the%20code%20would%20repair%20formats%20erased%20by%20the%20paste.%26nbsp%3B%20I%20will%20consider%20doing%20it%20that%20way%2C%20but%20it%20will%20be%20messy%20because%20the%20user%20might%20paste%20into%20several%20columns%20of%20data%2C%20and%20each%20column%20has%20a%20different%20background%20color.%26nbsp%3B%20I%20was%20hoping%20to%20avoid%20that.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1605178%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1605178%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382386%22%20target%3D%22_blank%22%3E%40perkin_warbeck%3C%2FA%3E%26nbsp%3B%20yes%20you%20are%20correct%20and%20I'm%20sure%20you%20read%20my%20sample%20code.%26nbsp%3B%20I%20delete%20any%20formats%20they%20brought%20in%20and%20then%20added%20the%20new%20formats.%26nbsp%3B%20I%20apply%20the%20new%20formats%20to%20all%20pasted%20cells%20but%20sounds%20like%20you%20have%20to%20be%20more%20selective.%3C%2FP%3E%3CP%3EHow%20about%20a%20completely%20different%20approach%20where%20you%20have%20them%20paste%20the%20data%20in%20a%20different%20sheet%20and%20then%20you%20use%20VBA%20to%20copy%20and%20paste%20values%20only.%3C%2FP%3E%3CP%3EOr%20if%20they%20past%20in%20a%20different%20sheet%20then%20you%20just%20use%20a%20formula%20to%20reference%20that%20data%20from%20the%20sheet%20with%20the%20formats.%3C%2FP%3E%3CP%3EYou%20can%20also%20use%20on%20selection%20in%20VBA%20to%20determine%20where%20they%20want%20to%20paste%20the%20data%20and%20then%20use%20VBA%20to%20paste%20values%20or%20give%20them%20a%20pop-up.%26nbsp%3B%20You%20can%20lock%20the%20entire%20sheet%20but%20allow%20selection%20so%20they%20can%20paste%20anywhere%20but%20VBA%20would%20do%20the%20work%20instead.%3C%2FP%3E%3CP%3EThere%20are%20many%20ways%20to%20skin%20a%20cat%20in%20excel%2C%20it's%20hard%20to%20suggest%20the%20best%20option%20for%20you%20without%20knowing%20more%20about%20the%20application.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1605662%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1605662%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F570951%22%20target%3D%22_blank%22%3E%40mtarler%3C%2FA%3E%26nbsp%3BI%20should%20have%20mentioned%20that%20I%20am%20very%20new%20to%20VBA%2C%20so%20I'm%20trying%20out%20most%20of%20these%20things%20for%20the%20first%20time.%26nbsp%3B%20I%20think%20you%20have%20pointed%20me%20in%20a%20a%20couple%20of%20good%20directions.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERepairing%20the%20formatting%20is%20certainly%20one%20way%20to%20do%20it.%26nbsp%3B%20I%20don't%20mind%20the%20application%20being%20complex%20if%20it%20spares%20the%20user%20from%20having%20to%20remember%20to%20Paste%20Values.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20you%20say%20%22%3CSPAN%3EYou%20can%20also%20use%20on%20selection%20in%20VBA%20to%20determine%20where%20they%20want%20to%20paste%20the%20data%20and%20then%20use%20VBA%20to%20paste%20values.%22%20It's%20an%20interesting%20solution%2C%20but%20it%20would%20require%20another%20button%20to%20accomplish%20the%20paste.%26nbsp%3B%20I'd%20like%20users%20to%20be%20able%20to%20paste%20normally.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20will%20mark%20your%20response%20as%20%22Best%20Response%22%20because%20you%20have%20given%20me%20several%20good%20ideas.%20Thanks!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1605710%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1605710%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F570951%22%20target%3D%22_blank%22%3E%40mtarler%3C%2FA%3E%26nbsp%3BI've%20been%20thinking%20more%20about%20your%20solution....%26nbsp%3B%20Repairing%20the%20formatting%20after%20a%20paste%20would%20be%20easier%20if%20each%20cell%20somehow%20%22remembered%22%20its%20own%20proper%20formatting.%26nbsp%3B%20This%20could%20be%20done%20by%20storing%20a%20duplicate%20of%20the%20original%20data%20area%20(with%20properly%20formatted%20cells)%20on%20a%20private%20worksheet.%26nbsp%3B%20Then%20Worksheet_Change%20would%20simply%20access%20the%20correct%20formatting%20from%20the%20private%20worksheet%20and%20apply%20it%20to%20the%20each%20cell%20of%20the%20target.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1605739%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1605739%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382386%22%20target%3D%22_blank%22%3E%40perkin_warbeck%3C%2FA%3E%26nbsp%3B%20If%20that%20is%20your%20%22fixed%22%20formatting%20that%20should%20be%20pretty%20easy%20to%20%22hard%20code%22%20into%20the%20VBA.%26nbsp%3B%20You%20idea%20of%20making%20a%20copy%20to%20a%20hidden%20sheet%20would%20work%20and%20as%20long%20as%20it%20isn't%20too%20big%20a%20file%20shouldn't%20cause%20too%20much%20delay.%26nbsp%3B%20Here%20is%20an%20example%20of%20my%20last%20suggestion%20above%20where%20you%20lock%20the%20entire%20sheet%20and%20just%20paste%20the%20data%20where%20ever%20they%20click.%26nbsp%3B%20It%20isn't%20%22perfect%22%20but%20could%20work%20for%20you%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_SelectionChange(ByVal%20Target%20As%20Range)%0A%20%20%20%20Const%20noPasteCell%20%3D%20%22%24A%241%22%0A%20%20%20%20confirm%20%3D%20MsgBox(%22Paste%20your%20data%20here%3F%22%2C%20vbYesNo)%0A%20%20%20%20On%20Error%20GoTo%20fail%0A%20%20%20%20If%20confirm%20%3D%20vbYes%20Then%0A%20%20%20%20%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20%20%20%20%20ActiveSheet.Unprotect%0A%20%20%20%20%20%20%20%20Target.PasteSpecial%20(xlPasteValues)%0A%20%20%20%20%20%20%20%20ActiveSheet.Protect%0A%20%20%20%20End%20If%0Afail%3A%0A%20%20%20%20Range(noPasteCell).Select%0A%20%20%20%20Application.EnableEvents%20%3D%20True%0A%20%20%20%20If%20Err%20Then%0A%20%20%20%20%20%20%20%20MsgBox%20(%22Paste%20Failed.%20%20Check%20your%20copy%20and%20try%20again.%22)%0A%20%20%20%20%20%20%20%20Err.Clear%0A%20%20%20%20End%20If%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EI%20defined%20%22noPasteCell%22%20as%20A1%20just%20as%20a%20place%20to%20auto%20move%20the%20selection%20to%20so%20it%20is%20intuitive%20for%20them%20to%20click%20where%20they%20want%20to%20paste%20the%20data.%26nbsp%3B%20You%20could%20add%20additional%20checks%20to%20make%20sure%20they%20aren't%20pasting%20data%20in%20incorrect%20areas.%26nbsp%3B%20You%20could%20also%20add%20a%20Password%20to%20prevent%20them%20from%20circumventing%20it%20if%20you%20wanted.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1606035%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20lock%20cell%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1606035%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F570951%22%20target%3D%22_blank%22%3E%40mtarler%3C%2FA%3E%26nbsp%3BI%20just%20lost%20a%20long%20reply%20because%20%22authentication%20failed%22%20when%20I%20posted.%26nbsp%3B%20So%20I'll%20re-type%20an%20abbreviated%20version.%26nbsp%3B%20I%20decided%20to%20go%20with%20partial%20locking%2C%20rather%20than%20full%20sheet%20locking.%20Paste%20fails%20if%20the%20clipboard%20data%20would%20overlap%20onto%20locked%20cells%2C%20which%20is%20a%20very%20desirable%20behavior.%26nbsp%3B%20There%20is%20no%20partial%20paste.%26nbsp%3B%20Since%20Excel%20is%20protecting%20the%20worksheet%2C%20there%20is%20no%20need%20to%20take%20the%20extra%20precautions%20in%20your%20example.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20are%20also%20controls%20on%20the%20worksheet%20(screenshot).%26nbsp%3B%20I%20unlocked%20these%20so%20they%20can%20be%20used%20when%20the%20sheet%20is%20protected.%26nbsp%3B%20Since%20those%20controls%20call%20macros%20that%20modify%20locked%20cells%2C%20the%20macros%20will%20have%20to%20temporarily%20unprotect%20the%20sheet.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

When a user pastes data onto a worksheet, how can I prevent the pasted data from erasing the formatting (cell borders, background color, number formats, etc.)?

 

The following method (which I found in several places on the web) does not work:

 

  1. Select all cells on the worksheet
  2. Home tab -> Cells group -> Format -> Format Cells... -> uncheck "Locked" -> OK
  3. Home tab -> Cells group -> Format -> Protect Sheet -> make sure "format cells" is unchecked" and do not enter a password -> OK (see screenshot below)

After doing this, the formatting groups in the Home tab are all grayed out (good).  However, when I paste into the sheet, the formatting in the pasted range is erased.  What am I doing wrong?

7 Replies

@perkin_warbeck I don't know that you can get around that.  You could do it using VBA.  I don't know what sort of formatting you have but here is a sample of what you might do in VBA (I assume you still lock the worksheet to make sure they only paste where you want them to paste).  The following VBA would be added to the specific sheet you want to control in VBA.

Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Target
        .ClearFormats
        .Font.FontStyle = "Times New Roman"
        .Font.Bold = True
        .Interior.Color = RGB(250, 250, 0)
        .Locked = False
    End With
    Application.EnableEvents = True
    ActiveSheet.Protect
End Sub

 

@mtarler My application is written entirely in VBA, and I am already handling Worksheet_Change events.  My understanding is that Worksheet_Change is called AFTER the change occurs.  In that case, the code would repair formats erased by the paste.  I will consider doing it that way, but it will be messy because the user might paste into several columns of data, and each column has a different background color.  I was hoping to avoid that.

Best Response confirmed by perkin_warbeck (Contributor)
Solution

@perkin_warbeck  yes you are correct and I'm sure you read my sample code.  I delete any formats they brought in and then added the new formats.  I apply the new formats to all pasted cells but sounds like you have to be more selective.

How about a completely different approach where you have them paste the data in a different sheet and then you use VBA to copy and paste values only.

Or if they past in a different sheet then you just use a formula to reference that data from the sheet with the formats.

You can also use on selection in VBA to determine where they want to paste the data and then use VBA to paste values or give them a pop-up.  You can lock the entire sheet but allow selection so they can paste anywhere but VBA would do the work instead.

There are many ways to skin a cat in excel, it's hard to suggest the best option for you without knowing more about the application.

@mtarler I should have mentioned that I am very new to VBA, so I'm trying out most of these things for the first time.  I think you have pointed me in a a couple of good directions. 

 

Repairing the formatting is certainly one way to do it.  I don't mind the application being complex if it spares the user from having to remember to Paste Values. 

 

But you say "You can also use on selection in VBA to determine where they want to paste the data and then use VBA to paste values." It's an interesting solution, but it would require another button to accomplish the paste.  I'd like users to be able to paste normally.

 

I will mark your response as "Best Response" because you have given me several good ideas. Thanks!

@mtarler I've been thinking more about your solution....  Repairing the formatting after a paste would be easier if each cell somehow "remembered" its own proper formatting.  This could be done by storing a duplicate of the original data area (with properly formatted cells) on a private worksheet.  Then Worksheet_Change would simply access the correct formatting from the private worksheet and apply it to the each cell of the target.

@perkin_warbeck  If that is your "fixed" formatting that should be pretty easy to "hard code" into the VBA.  You idea of making a copy to a hidden sheet would work and as long as it isn't too big a file shouldn't cause too much delay.  Here is an example of my last suggestion above where you lock the entire sheet and just paste the data where ever they click.  It isn't "perfect" but could work for you:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const noPasteCell = "$A$1"
    confirm = MsgBox("Paste your data here?", vbYesNo)
    On Error GoTo fail
    If confirm = vbYes Then
        Application.EnableEvents = False
        ActiveSheet.Unprotect
        Target.PasteSpecial (xlPasteValues)
        ActiveSheet.Protect
    End If
fail:
    Range(noPasteCell).Select
    Application.EnableEvents = True
    If Err Then
        MsgBox ("Paste Failed.  Check your copy and try again.")
        Err.Clear
    End If
End Sub

I defined "noPasteCell" as A1 just as a place to auto move the selection to so it is intuitive for them to click where they want to paste the data.  You could add additional checks to make sure they aren't pasting data in incorrect areas.  You could also add a Password to prevent them from circumventing it if you wanted.

@mtarler I just lost a long reply because "authentication failed" when I posted.  So I'll re-type an abbreviated version.  I decided to go with partial locking, rather than full sheet locking. Paste fails if the clipboard data would overlap onto locked cells, which is a very desirable behavior.  There is no partial paste.  Since Excel is protecting the worksheet, there is no need to take the extra precautions in your example. 

 

There are also controls on the worksheet (screenshot).  I unlocked these so they can be used when the sheet is protected.  Since those controls call macros that modify locked cells, the macros will have to temporarily unprotect the sheet.