Search code examples
excelcellspreadsheethidden

Forbid selecting or copying hidden cells in Excel


I am developing some spreadsheets for other users, where there are several columns I don't want the user to see. So I hide them and lock the spreadsheet down. The reason that why I hide them rather than delete them is for further development. However, I find that the user can select a bigger range across the hidden cells, copy and paste to get the hidden information. Is there anyway I can FORBID the user to do so? The methods I can searched online are all about how to select visible cells only, but not force them to do so and prevent them to get the hidden information. Could anyone help me with this?


Solution

  • With VBA, you might be able to leverage the Selection Change event, test whether the selection spans those hidden columns using Intersect, and then select a separate, visible section of the worksheet instead.

    For example, if columns D:F are hidden,

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Not Intersect(Target, Me.Columns("D:F")) Is Nothing Then
            MsgBox "Your selection includes hidden column(s). BAD!", vbCritical
            Me.Range("A1").Select
        End If
    End Sub
    

    This code goes in the worksheet's code module.