+ Reply to Thread
Results 1 to 2 of 2

VBA code for Disable/Enable cells

Hybrid View

  1. #1
    Registered User
    Join Date
    03-19-2020
    Location
    India
    MS-Off Ver
    Microsoft Pro 365
    Posts
    9

    VBA code for Disable/Enable cells

    In this attached excel , I have 3 types of target such as weekly , monthly , and yearly . User has to choose either one option and fill the target accordingly .

    If the user is selecting target type as yearly , then monthly target cell (B3) should be disabled and user could update the target only in yearly target column.

    If the user is selecting target type as monthly , then yearly target cell (B4) should be disabled and user could update the target only in monthly target column.

    If the user is selecting target type as Weekly , then both Monthly target and yearly target should be disabled(B3 and B4 ).

    Can someone help me to find the code for above scenario..

    Please find the attached excel for your reference.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: VBA code for Disable/Enable cells

    Start by unprotecting the range B2:B5. Then protect the sheet using a password of your choice. Next copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Change the password (in red) to match your actual password. Close the code window to return to your sheet. Make a selection in B2.
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
        ActiveSheet.Unprotect Password:="MyPassword"
        Select Case Target.Value
            Case "Yearly"
                Range("B3").Locked = True
                Range("B4").Locked = False
            Case "Monthly"
                Range("B3").Locked = False
                Range("B4").Locked = True
            Case "Weekly"
                Range("B3:B4").Locked = True
        End Select
        With ActiveSheet
            .Protect Password:="MyPassword"
            .EnableSelection = xlUnlockedCells
        End With
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Is there vba code which can disable/enable switching between open workbooks
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2016, 05:37 AM
  2. VBA Code to Know Macro Enable or Disable when Open workbook
    By GOLDENEXCEL in forum Excel General
    Replies: 3
    Last Post: 10-09-2015, 08:52 AM
  3. VBA to check cell value and enable/disable two other cells in same row
    By dexterz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2015, 09:09 AM
  4. Enable/Disable vba code
    By henk400 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2015, 08:16 AM
  5. [SOLVED] Code to disable and enable indentations
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2014, 01:38 PM
  6. Create A Button In Document To Disable/Enable A Specific Line of Code
    By BLK306 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-05-2014, 02:12 PM
  7. enable and disable cells based on the value of another cell
    By Emma_B in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-09-2013, 08:06 AM

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