我需要根据G8,H8,I8,J8和K8中规定的日期来锁定列。
如果G8没有今天,我需要通过G44锁定G9。
如果今天是星期日,而G8是星期日,则G9至G44应该打开。否则,数据范围应锁定。
这应该从G到K的所有列完成。我该怎么做? 预先感谢一百万。
Private Sub worksheet_Change(ByVal Target As Range)
Dim col As Range
With ThisWorkbook.Sheets("Sheet1")
.Unprotect "ABCDE"
For Each col In .UsedRange.Columns
col.EntireColumn.Locked = col.Range("A1").Value < Date
Next col
.Protect "ABCDE"
.EnableSelection = xlNoRestrictions
End With
End Sub
答案 0 :(得分:0)
欢迎来到SO。您的代码看起来不错,您需要做的就是添加更多If
条件。试试看:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim col As Long
Dim tDate As Date
tDate = Date 'today's date
With ThisWorkbook.Sheets("Sheet1")
'.Unprotect "ABCDE"
For col = 0 To .Range("G9:K44").Columns.Count - 1
'check if row 8 doesn't have today's date
If .Range("G8").Offset(0, col).Value <> tDate Then
'if today is Sunday and row 8 has Sunday
If Format(tDate, "dddd") = "Sunday" And Format(.Range("G8").Offset(0, col).Value, "dddd") = "Sunday" Then
.Range("G8:G44").Offset(0, col).EntireColumn.Locked = False
Else
.Range("G8:G44").Offset(0, col).EntireColumn.Locked = True
End If
End If
Next col
'.Protect "ABCDE"
'.EnableSelection = xlNoRestrictions
End With
End Sub
将tDate
更改为其他日期以测试代码,例如tDate = '2020-02-16'
(星期日)。