填充其他单元格时阻止Excel单元格

时间:2018-06-21 02:39:44

标签: vba excel-vba excel

我尝试使用系统日期进行更新,然后阻止该日期,以便一旦更新其他四个单元格就无法更改日期。例如,如果您在K列上完成一行,则将添加系统日期的O列行,此后需要阻止该单元格,因此无论您在K列上进行什么机会,O列的日期始终是和第一次一样。我尝试通过循环执行此操作,但发现循环编写不正确。谁能帮我解决循环问题,或者还有其他方法可以解决此问题?

Do While Range("O:O").Locked = False
'autofill columna O con fecha en base a colK
Dim WorkRng1 As Range
Dim Rng1 As Range
Dim xOffsetColumn1 As Integer
Set WorkRng1 = Intersect(Application.ActiveSheet.Range("K:K"), Target)
xOffsetColumn1 = 4
If Not WorkRng1 Is Nothing Then
    Application.EnableEvents = False
    For Each Rng1 In WorkRng1
        If Not VBA.IsEmpty(Rng1.Value) Then
            Rng1.Offset(0, xOffsetColumn1).Value = Now
            Rng1.Offset(0, xOffsetColumn1).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Rng1.Offset(0, xOffsetColumn1).Locked = True
        End If
    Next
    Application.EnableEvents = True
End If
'autofill columna O con fecha en base a colL
Dim WorkRng2 As Range
Dim Rng2 As Range
Dim xOffsetColumn2 As Integer
Set WorkRng2 = Intersect(Application.ActiveSheet.Range("L:L"), Target)
xOffsetColumn2 = 3
If Not WorkRng2 Is Nothing Then
    Application.EnableEvents = False
    For Each Rng2 In WorkRng2
        If Not VBA.IsEmpty(Rng2.Value) Then
            Rng2.Offset(0, xOffsetColumn2).Value = Now
            Rng2.Offset(0, xOffsetColumn2).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Rng2.Offset(0, xOffsetColumn2).Locked = True
        End If
    Next
    Application.EnableEvents = True
End If
'autofill columna O con fecha en base a colM
Dim WorkRng3 As Range
Dim Rng3 As Range
Dim xOffsetColumn3 As Integer
Set WorkRng3 = Intersect(Application.ActiveSheet.Range("M:M"), Target)
xOffsetColumn3 = 2
If Not WorkRng3 Is Nothing Then
    Application.EnableEvents = False
    For Each Rng3 In WorkRng3
        If Not VBA.IsEmpty(Rng3.Value) Then
            Rng3.Offset(0, xOffsetColumn3).Value = Now
            Rng3.Offset(0, xOffsetColumn3).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Rng3.Offset(0, xOffsetColumn3).Locked = True
        End If
    Next
    Application.EnableEvents = True
End If
'autofill columna O con fecha en base a colN
Dim WorkRng4 As Range
Dim Rng4 As Range
Dim xOffsetColumn4 As Integer
Set WorkRng4 = Intersect(Application.ActiveSheet.Range("N:N"), Target)
xOffsetColumn4 = 1
If Not WorkRng4 Is Nothing Then
    Application.EnableEvents = False
    For Each Rng4 In WorkRng4
        If Not VBA.IsEmpty(Rng4.Value) Then
            Rng4.Offset(0, xOffsetColumn4).Value = Now
            Rng4.Offset(0, xOffsetColumn4).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Rng4.Offset(0, xOffsetColumn4).Locked = True
        End If
    Next
    Application.EnableEvents = True
End If
Loop

0 个答案:

没有答案