首页 文章

锁定特定月份的行

提问于
浏览
1

我的数据包含包含Month的单元格A1 .

A2:Z999:员工详细信息 . P栏:加入日期 .

我想编写代码,当P> A1时,它应该锁定该员工的行,因为它是旧数据 . 只有没有日期的行或空行999才能为空 .

请帮忙!现在我的代码锁定了一切 .

Dim DestSh As Worksheet
Dim lastrow As Long


Set DestSh = Sheets("Consultant & Teacher")

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

    End If

    If Range("A1").Value = "April" Then
    .Unprotect Password:="MyPassword"
    .Cells.Locked = False
    .Range("A2:Z" & lastrow).Locked = True

1 回答

  • 0

    我不太确定你用现有代码想要完成什么,但纯粹基于你解释的内容,我认为这应该可以解决问题:

    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 = 2 To lastrow
           'if your conditions are met
           If Month(.Cells(i, 16)) > Month(.Cells(1, 1)) Then
              .Range("A" & i).EntireRow.Cells.Locked = True 'lock the row
           End If
        Next i
    End With
    

相关问题