any one check this macro

  • when i am running this macro to protect and unprotect in given range it is protecting as well and when i am again running this macro to unprotect it's not unprotecting



  • What do you think the code is doing? This code actually does nothing but check if a cell is empty.


    Code
    1. For Each MyCell In Rng
    2. If MyCell.Value = "" Then


    The code loops through the cells and if it finds a cell that isn't blank it locks it unprotects the sheet , locks the cell then protects the sheet. That is unnecessary. What is expected to be in cells - formulas, numbers or text? or text


    This will find and lock all cells containing text in the range.


  • when i am run this macro it's protecting whole active sheet and again run again macro it's don't unprotecting i want to protect and unprotect just a in range A1 to D20



  • Have you set up protection for the sheet correctly before running the macro? If you don't unlock all the cells first then they are locked by default.

  • This is a working example. You must prepare the cells correctly before running, this is true when protecting sheets with or without VBA.