更新一列中多个单元格,如果多个单元格中的另一列改

时间:2019-01-30 22:31:35

标签: excel vba

我在Excel中创建一个任务列表会输入当前日期,日期和时间将根据用户的另一列指示任务进入一个“X”一列完成。

从网上找到一些代码,我已经在片材代码编写的宏,这样,当一个单元被更新为包含在一个值column E。当前日期,日期和时间填充在column B中。如果删除单元格内容,它也会清除日期。这一切都很好,很花哨。

现在我有如果用户选择的范围在column E删除多个问题“×” S在一个时间。宏出错,并说在行上有Type Mismatch

If Target.Value = 0 Then

如果用户选择一个区域并从中删除“ x”,如何编辑我的代码以使其也考虑?

我如何做到这一点,以便仅输入“ x”会触发代码放置日期信息?而不是任何形式的内容更改。

请注意:偏移现在被设置为3列的权利,因为我不希望它更新B column只是还没有。当此代码正常工作时,我会将偏移量切换为-3。

    Sub worksheet_change(ByVal Target As Range)

    Dim DayName As String
    Dim lastrow As Long
    Dim currentRow As Long

    'Sets the current time as TimeNow
    TimeNow = Now()
    'Gets the first 3 characters of the weekday and set it as DayName
    DayName = Left(Format(Date, "dddd"), 3)
    'Gets the last row # of the "TASK" column that isn't blank. (Continuously updates as new tasks are added)
    lastrow = Range("D" & Rows.Count).End(xlUp).row

        If Not Intersect(Target, Range("E2:E" & lastrow)) Is Nothing Then

            If Target.Value = 0 Then
                ' The offset is set to 3 columns to the right for now because I don't want it to update the B column just yet. When this code is working properly I will switch the offset to -3.
                Target.Offset(, 3) = vbNullString
            Else
                Target.Offset(, 3) = DayName & " " & TimeNow

            End If

        End If

End Sub

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是在任何代码之前捕获多个选择并退出事件。我建议继续参加变更活动。

 SELECT JSON_EXTRACT(s.jsoncol, CONCAT('$.',k.bar)) AS foo
   FROM mytable s
  CROSS
   JOIN ( SELECT 'key2' AS bar ) k

答案 1 :(得分:0)

不要用创可贴解决方案解决问题。处理Target中每个更改的单元格是一个单元格还是一千零一十。

Option Explicit

Sub worksheet_change(ByVal Target As Range)

    If Not Intersect(Target, Range("E:E"), UsedRange.Offset(1, 0)) Is Nothing Then
        On Error GoTo bye_bye
        Application.EnableEvents = False
        Dim t As Range
        For Each t In Intersect(Target, Range("E:E"), UsedRange.Offset(1, 0))
            'The offset is set to 3 columns to the right for now
            ' because I don't want it to update the B column just yet.
            'When this code is working properly I will switch the offset to -3.
            Select Case LCase(t.Value)
                Case vbNullString
                    t.Offset(, 3) = vbNullString
                Case "x"
                    t.Offset(, 3) = Format(Now, "ddd hh:mm:ss")
                Case Else
                    'do nothing?
            End Select
        Next t

    End If

bye_bye:
    Application.EnableEvents = True

End Sub

在更改工作表上任何单元格的值之前,请记住禁用事件处理,否则worksheet_change将在其自身之上运行。