简体   繁体   中英

How to lock a column until it's last row with data

I have date mentioned in cell A1, ex - "May".

I am now trying to lock rows 2-last with column Z which mentions date of joining of each employee and compares it to A1.

If month of this cell Z is > A1 then I am trying to lock the row. Not sure what to do.

Below code doesnt help :

Sub Lockrow()
Dim DestSh As Worksheet
Dim lastrow As Long
Dim i As Integer

Set DestSh = Sheets("Consultant & Teacher")

With DestSh
    'finds the last row with data on A column
    lastrow = Range("A65536").End(xlUp).Row

    'parse all rows
    For i = 6 To lastrow
       'if your conditions are met
       If Month(.Cells(i, 26)) > Month(.Cells(1, 2)) Then
          .Range("A" & i).EntireRow.Cells.Locked = True 'lock the row
       End If
    Next i
End With

End Sub

Is this what you are trying?

Sub Sample()
    Dim DestSh As Worksheet
    Dim lastrow As Long

    '~~> Change this as applicable
    Set DestSh = Sheets("Sheet1")

    With DestSh
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lastrow = .Columns("A:C").Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
        Else
            MsgBox "Insufficient rows"
            Exit Sub
        End If

        .Unprotect "MyPassword"
        .Cells.Locked = False

        .Range("A6:C" & lastrow).Locked = True

        .Protect "MyPassword"
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM