CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    Making Excel cells read-only using VBA

    Hai Code gurus,I got a problem. I am using VBA editor in Excel. I have written
    a macro to get the data from ORACLE and print it on EXCEL. Now I want to make some cells read-only. Nobody should be allowed to change those particular cells. How can I do that?. Pls give your solution

    With Regards

    Ravi


  2. #2
    Join Date
    Dec 1999
    Posts
    4

    Re: Making Excel cells read-only using VBA

    As far as I can tell (and I'm no guru) every cell in a worksheet is by default "locked". Now, this means that the contents cannot be changed. However, in order for the lock to work, you have to protect the sheet. So, assuming you want to only protect certain cells and give access to all the others, it would go like this:

    --------------------<snip>------------------------

    Sub CellLocker()
    Cells.Select
    ' unlock all the cells
    Selection.Locked = false
    ' next, select the cells (or range) that you want to make read only,
    ' here I used simply A1
    Range("A1").Select
    ' lock those cells
    Selection.Locked = true
    ' now we need to protect the sheet to restrict access to the cells.
    ' I protected only the contents you can add whatever you want
    ActiveSheet.Protect DrawingObjects:=false, Contents:=true, Scenarios:=false
    End Sub



    --------------------</snip>-----------------------

    once again, I'm no guru but I hope this helps. I'm not 100% sure, but I think you should be able to read values (when I lock them I know I can at least copy and paste values).

    Hannibal
    [email protected]


  3. #3
    Guest

    Re: Making Excel cells read-only using VBA

    Dear Hannibal,

    Thanks a lot for your suggestion. It's just fantastic and solved my problem.

    Once again thanking you,

    With Regards

    Ravi


  4. #4
    Join Date
    Feb 2009
    Posts
    1

    Re: Making Excel cells read-only using VBA

    There is functionality is excel that we can stop users from copying the contents of cells, how can we implement the same using VB code. Kindly help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured