+ Reply to Thread
Results 1 to 19 of 19

hide and lock cells with formula using vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    hide and lock cells with formula using vba

    hi,

    anybody knows how to hide formulas and lock that cell with formula using a macro? is this possible to do for the entire worksheets of the workbook?

    thanks,

    stoey
    Last edited by stoey; 10-08-2011 at 10:26 AM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: hide and lock cells with formula using vba

    Off the top

    For Each ws In ActiveWorkbook.Worksheets
    
        For Each cell In ws.UsedRange
    
            If cell.HasFormula Then
    
                cell.Locked = True
                cell.Hidden = True
            End If
        Next cell
    Next ws

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    A possible way without looping the cells
    Option Explicit
    
    Sub LockFormulae()
        Dim rngHasFormula As Range
        Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Worksheets
            ws.Unprotect
            Set rngHasFormula = ws.UsedRange.SpecialCells(xlCellTypeFormulas)
            With rngHasFormula
                .Locked = True
                .FormulaHidden = True
            End With
            ws.Protect
        Next
    End Sub
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: hide and lock cells with formula using vba

    thanks for the quick reply bob and maarcol,

    i dont really know where to put the coding from Bob so i tried marcol's macro...I clicked Thisworkbook from the Microsoft Visual Basic Editor window and selected (General) followed by (Declarations) before placing the code...but it did not work...i can still see the formulas...i might be doing something wrong here...please help

    regards,

    stoey

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    The code should be in a standard module not the workbook module.

    1/. In Excel press Alt+f11 this will open the VBa editor.

    2/. Select the "Insert" tab > Module

    3/. Paste all of the given code in the resultant pane/window.

    4/. Save the file and close the VBa editor

    5/. In Excel (2007) Developer > Macros.... select the one you need then Press "Run"

  6. #6
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: hide and lock cells with formula using vba

    alright just did your instructions marcol and i received an error message it says "Run time error 1004: no cells were found...any idea?

  7. #7
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: hide and lock cells with formula using vba

    when i click on debug it points right here
    Set rngHasFormula = ws.UsedRange.SpecialCells(xlCellTypeFormulas)

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: hide and lock cells with formula using vba

    You can put this into any module (sheet, workbook or macro module)
    Remove 'Option Explicit'

    Sub snb()
     on error resume next
     
     For Each sh In sheets
       with sh.cells.SpecialCells(xlCellTypeFormulas)
         if err.number=0 then 
           sh.unprotect
           .Locked = True
           .FormulaHidden = True
         end if
       End With
       err.clear
     Next
    End Sub



  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: hide and lock cells with formula using vba

    Do you have any cells with formulas in them? If not then you are going to get an error message. You can get around this by adding:

    On Error Resume Next
    right after the declarations, but you should be sure that this is your only problem before supressing all error messages.

    Hope this helps.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: hide and lock cells with formula using vba

    I have to learn to type faster . My post is just a redundancy of what Marcol already said.

    abousetta

  11. #11
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: hide and lock cells with formula using vba

    whoa! i was so overwhelm with all of you guys thank you very much for helping me...

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    Okay that means there are no formulae in that particular worksheet.

    Try this code
    Option Explicit
    
    Sub LockFormulae()
        Dim rngHasFormula As Range
        Dim ws As Worksheet
    
        On Error Resume Next
        For Each ws In ActiveWorkbook.Worksheets
            ws.Unprotect
            Set rngHasFormula = ws.UsedRange.SpecialCells(xlCellTypeFormulas)
            If Err.Number <> 0 Then
                Err.Clear
            Else
                With rngHasFormula
                    .Locked = True
                    .FormulaHidden = True
                End With
            End If
            ws.Protect
        Next
        On Error GoTo 0
    End Sub
    Last edited by Marcol; 10-08-2011 at 09:58 AM.

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    @ snb
    You can put this into any module (sheet, workbook or macro module)
    Remove 'Option Explicit'
    There are many authoritative Excel coders that consider that to be bad practice.

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: hide and lock cells with formula using vba

    I consider it to be a superstition.
    Arguments can convince me, authority can't.
    Last edited by snb; 10-08-2011 at 04:45 PM.

  15. #15
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: hide and lock cells with formula using vba

    Quote Originally Posted by snb View Post
    I consider it to be a superstition.

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: hide and lock cells with formula using vba

    Quote Originally Posted by snb View Post
    I consider it [Option Explicit] to be a superstition.
    Arguments can convince me, authority can't.
    The number of misspellings that Option Explicit has caught for me have convinced me to use it.

    I could see someone who didn't feel they needed it, but I can't think of any argument why its presence is a bad idea.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  17. #17
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    @ snb
    I look forward to your treatise on this subject, on the back of a postage stamp should suffice.

    @ mikerickson if you can't spell "j" what hope is there for you? ...

  18. #18
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: hide and lock cells with formula using vba

    Its not a lack of hope that misspells J, its an excess of hop.

  19. #19
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: hide and lock cells with formula using vba

    Beer? ... yum yum! ... burp ... ...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1