簡體   English   中英

如何將列鎖定到包含數據的最后一行

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

我在小區A1中提到過日期,前 - “五月”。

我現在正試圖用列Z鎖定2-last行,其中提到每個員工的加入日期並將其與A1進行比較。

如果這個單元格Z的月份是> A1,那么我試圖鎖定該行。 不知道該怎么辦。

下面的代碼沒有幫助:

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

這是你在嘗試什么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM