+ Reply to Thread
Results 1 to 25 of 25

Lock and protect selected excel rows with VBA code

Hybrid View

SmallJack Lock and protect selected... 04-13-2015, 05:29 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 05:32 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 05:53 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 05:55 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 05:59 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 06:05 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 06:16 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 06:31 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 06:35 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 06:54 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 06:50 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 06:58 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 07:25 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 07:28 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 07:38 AM
NeedForExcel Re: Lock and protect selected... 04-13-2015, 07:44 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 07:55 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 08:37 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 09:28 AM
HaHoBe Re: Lock and protect selected... 04-13-2015, 09:47 AM
SmallJack Re: Lock and protect selected... 04-13-2015, 09:36 PM
HaHoBe Re: Lock and protect selected... 04-13-2015, 11:36 PM
SmallJack Re: Lock and protect selected... 04-15-2015, 02:14 AM
SmallJack Re: Lock and protect selected... 04-15-2015, 10:58 AM
SmallJack Re: Lock and protect selected... 04-15-2015, 09:06 PM
  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Lock and protect selected excel rows with VBA code

    Check it out now
    Attached Files Attached Files
    Cheers!
    Deep Dave

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Lock and protect selected excel rows with VBA code

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim lngCtr As Long
    
    Const cstrPW As String = "password"
    
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("B2:D11")) Is Nothing Then
    
      For lngCtr = 2 To 11
        If WorksheetFunction.CountBlank(Range("B" & lngCtr & ":D" & lngCtr)) = 0 Then
          ActiveSheet.Unprotect cstrPW
          Range("B" & lngCtr & ":D" & lngCtr).Locked = True
        Else
          Range("B" & lngCtr & ":D" & lngCtr).Locked = False
        End If
      Next lngCtr
      ActiveSheet.Protect cstrPW
    End If
    
    End Sub
    @NeedForExcel:
    Please enter Test into Cell B11 alone and run your code in your attachment. I doubt that all empty rows between 3 and 10 should be locked as well.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Lock and protect selected excel rows with VBA code

    Quote Originally Posted by HaHoBe View Post
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim lngCtr As Long
    
    Const cstrPW As String = "password"
    
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("B2:D11")) Is Nothing Then
    
      For lngCtr = 2 To 11
        If WorksheetFunction.CountBlank(Range("B" & lngCtr & ":D" & lngCtr)) = 0 Then
          ActiveSheet.Unprotect cstrPW
          Range("B" & lngCtr & ":D" & lngCtr).Locked = True
        Else
          Range("B" & lngCtr & ":D" & lngCtr).Locked = False
        End If
      Next lngCtr
      ActiveSheet.Protect cstrPW
    End If
    
    End Sub
    @NeedForExcel:
    Please enter Test into Cell B11 alone and run your code in your attachment. I doubt that all empty rows between 3 and 10 should be locked as well.

    Ciao,
    Holger
    Hi Holger,

    I have kept that in mind, however keeping in mind, normally data entry will happen one row after another, that's why like that..

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Lock and protect selected excel rows with VBA code

    Hi, NeedForExcel,

    no need for a full quote here - maybe have a look into the Forum Rules (#12).

    Any entry into Column B would have to leave Column C and D open for more entries in that row or how will you fill the entries in these columns when the row is locked?

    Ciao,
    Holger

  5. #5
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Lock and protect selected excel rows with VBA code

    Quote Originally Posted by HaHoBe View Post
    Any entry into Column B would have to leave Column C and D open for more entries in that row or how will you fill the entries in these columns when the row is locked?
    Agreed! But the user will lock the Row only when he finishes filling all the 4 Columns on that particular row wont he? Atlease that's what I think.

    Best if the OP states if he requires something otherwise.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Lock and protect selected excel rows with VBA code

    Hi, NeedForExcel,

    the yellow highlighted area holds Columns B to D (which is 3 Columns) and the rows 2 to 11. With your code (which could be started at any time even if not entry or change was made) the whole area between B2 and F and the last entry row in Column B is blocked. I can´t agree on that from what I read that from post #10.

    Ciao,
    Holger

+ 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] How lock rows and protect cells based on date in first column
    By udaybrown7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-27-2014, 05:47 PM
  2. protect and lock rows
    By tkraju in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2014, 11:46 PM
  3. [SOLVED] How to lock/protect a cell that is selected in a macro range
    By rwstarkey in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2014, 08:09 PM
  4. Marco to Lock/Unlock and Protect/Unprotect Multiple Excel Workbooks
    By Jgonza25 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-22-2013, 03:29 PM
  5. MAcro to lock and unlock cells depending on rows selected.
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2007, 11:04 PM

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