Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Making Excel Cells Read Only

Making Excel Cells Read Only

Scheduled Pinned Locked Moved C#
help
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dpasswat
    wrote on last edited by
    #1

    I have an application that creates an Excel workbook from a template. After the workbook is created I populate each Worksheet. I'm attempting to make the cells that contain headers and other data describing text read only. Here are my latest two attempts:

     Worksheet ws = m\_wkbk.Sheets\[1\];
     ws.Activate();
    
     // Attempt 1
     ws.Range\["A1", "E1"\].Style.Locked = true; // This runs but has no impact on the launched sheet.
    
     // Attempt 2
     Range range = (Range)ws.get\_Range(ws.Cells\[1, 1\], ws.Cells\[1, 5\]); // Runtime error
     range.Locked = true;
    
    M P R 3 Replies Last reply
    0
    • D dpasswat

      I have an application that creates an Excel workbook from a template. After the workbook is created I populate each Worksheet. I'm attempting to make the cells that contain headers and other data describing text read only. Here are my latest two attempts:

       Worksheet ws = m\_wkbk.Sheets\[1\];
       ws.Activate();
      
       // Attempt 1
       ws.Range\["A1", "E1"\].Style.Locked = true; // This runs but has no impact on the launched sheet.
      
       // Attempt 2
       Range range = (Range)ws.get\_Range(ws.Cells\[1, 1\], ws.Cells\[1, 5\]); // Runtime error
       range.Locked = true;
      
      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      Excel only locks the locked cells when a password is put on the sheet. Every cell has the Locked attribute set on new sheets, you unlock cells you want to allow users to change then set a password on the sheet to protect the locked cells.

      Z P 2 Replies Last reply
      0
      • M Michael_Davies

        Excel only locks the locked cells when a password is put on the sheet. Every cell has the Locked attribute set on new sheets, you unlock cells you want to allow users to change then set a password on the sheet to protect the locked cells.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        Correct. :thumbsup:

        There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

        1 Reply Last reply
        0
        • M Michael_Davies

          Excel only locks the locked cells when a password is put on the sheet. Every cell has the Locked attribute set on new sheets, you unlock cells you want to allow users to change then set a password on the sheet to protect the locked cells.

          P Offline
          P Offline
          Patrice T
          wrote on last edited by
          #4

          A sheet can be protected without password.

          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

          M 1 Reply Last reply
          0
          • D dpasswat

            I have an application that creates an Excel workbook from a template. After the workbook is created I populate each Worksheet. I'm attempting to make the cells that contain headers and other data describing text read only. Here are my latest two attempts:

             Worksheet ws = m\_wkbk.Sheets\[1\];
             ws.Activate();
            
             // Attempt 1
             ws.Range\["A1", "E1"\].Style.Locked = true; // This runs but has no impact on the launched sheet.
            
             // Attempt 2
             Range range = (Range)ws.get\_Range(ws.Cells\[1, 1\], ws.Cells\[1, 5\]); // Runtime error
             range.Locked = true;
            
            P Offline
            P Offline
            Patrice T
            wrote on last edited by
            #5

            And what is the result of this ?

            ws.Range["A1", "E1"].Locked = true;

            Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

            D 1 Reply Last reply
            0
            • D dpasswat

              I have an application that creates an Excel workbook from a template. After the workbook is created I populate each Worksheet. I'm attempting to make the cells that contain headers and other data describing text read only. Here are my latest two attempts:

               Worksheet ws = m\_wkbk.Sheets\[1\];
               ws.Activate();
              
               // Attempt 1
               ws.Range\["A1", "E1"\].Style.Locked = true; // This runs but has no impact on the launched sheet.
              
               // Attempt 2
               Range range = (Range)ws.get\_Range(ws.Cells\[1, 1\], ws.Cells\[1, 5\]); // Runtime error
               range.Locked = true;
              
              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #6

              to make the Locked-Attribute working you have to protect your Sheet. For example :

              ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
              
              D 1 Reply Last reply
              0
              • P Patrice T

                A sheet can be protected without password.

                Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                M Offline
                M Offline
                Michael_Davies
                wrote on last edited by
                #7

                My apologise, your right, I just never used it that way always had passwords.

                1 Reply Last reply
                0
                • P Patrice T

                  And what is the result of this ?

                  ws.Range["A1", "E1"].Locked = true;

                  Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                  D Offline
                  D Offline
                  dpasswat
                  wrote on last edited by
                  #8

                  It runs but has no impact on the launched sheet.

                  P 1 Reply Last reply
                  0
                  • R Ralf Meier

                    to make the Locked-Attribute working you have to protect your Sheet. For example :

                    ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
                    
                    D Offline
                    D Offline
                    dpasswat
                    wrote on last edited by
                    #9

                    Thanks for all of the input. I ended up adjusting the protection settings in a template after locking the individual cells. It works like a champ now.

                    1 Reply Last reply
                    0
                    • D dpasswat

                      It runs but has no impact on the launched sheet.

                      P Offline
                      P Offline
                      Patrice T
                      wrote on last edited by
                      #10

                      dpasswat wrote:

                      It runs but has no impact

                      What means: "no impact" ? Does it set the locked cell property or not ?

                      Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups