锁定特定月份的行

时间:2016-10-05 11:22:17

标签: excel vba

我的数据包含包含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 :(得分: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