+ Reply to Thread
Results 1 to 4 of 4

VB code doesn't work when sheet is protected

  1. #1
    Registered User
    Join Date
    01-05-2022
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    VB code doesn't work when sheet is protected

    I have several cells that use vlookup to insert values based on a selection from a dropdown in a different cell. I locked the cells with the formulas and protect the sheet to preserve unintended formula corruption. However, when I protect the sheet, VB code that allows multiple values in a handful of unrelated cells stops working. I have very little VB knowledge/experience. Here is the code for multiple values:

    Dim Oldvalue As String
    Dim Newvalue As String

    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Target.Column = 13 Or Target.Column = 8 Or Target.Column = 14 Or Target.Column = 15 Or Target.Column = 18 Or Target.Column = 21 Then
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
    Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
    If Oldvalue = "" Then
    Target.Value = Newvalue
    Else
    If InStr(1, Oldvalue, Newvalue) = 0 Then
    Target.Value = Oldvalue & vbNewLine & Newvalue
    Else:
    Target.Value = Oldvalue
    End If
    End If
    End If
    End If
    Application.EnableEvents = True
    Exitsub:
    Application.EnableEvents = True

    Cells.Replace What:=Chr(10), Replacement:=";", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    End Sub

    I used to have Activsheet.Unprotect in the beginning, and Activesheet.protect at the end, but I removed it because users had to keep unprotecting the sheet too many times and it was both frustrating and unproductive.

    Thank you in advance.

    Regards,
    Nick

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,381

    Re: VB code doesn't work when sheet is protected

    Try to protect this way, which allows macro's to run

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-05-2022
    Location
    NYC
    MS-Off Ver
    2013
    Posts
    4

    Re: VB code doesn't work when sheet is protected

    Quote Originally Posted by JEC. View Post
    Try to protect this way, which allows macro's to run

    Please Login or Register  to view this content.

    JEC, you rock! Thank you, thank you, thank you. This has been a thorn in my side and for the users as well. I appreciate you taking the time to help others.

    Regards,
    Nick

    p.s. I tried to reply yesterday, but I don't see it in this thread so I am trying again.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,381

    Re: VB code doesn't work when sheet is protected

    Nice! You're welcome!
    Cheers!

+ 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. [Solved] Code to protect sheet doesn't work
    By RogerH in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-19-2020, 12:21 PM
  2. Sheet Code name doesn't work across workbooks
    By hemantparmar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-18-2019, 07:56 AM
  3. Testing single code, work fine. Put multiple codes in one sheet, one code doesn't work.
    By MayDay1988 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2017, 06:14 PM
  4. [SOLVED] [Solved] Looping Thru Sheet Code Doesn't Work. Please help
    By bernice620 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-21-2012, 03:22 PM
  5. Looping Thru Sheet Code Doesn't Work. Please help
    By bernice620 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-20-2012, 05:07 PM
  6. VBA code doesn't work when formula in sheet is removed
    By Marco-Kun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2009, 01:18 PM
  7. Macro doesn't work on UserInterfaceOnly-protected sheet
    By Kasama in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-08-2006, 05:25 AM

Tags for this Thread

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