简体   繁体   中英

Excel macro - type mismatch error

im a noob.

My macro adds a date when a cell value changes to "Closed". Specifically, when a cell value in column M changes to "Closed", it adds the date 2 cells to the left, in column K. Works perfectly, until i edit more than one cell in either column . If i do that, i get a 13 type mismatch error.

This sucks as it means an error comes up each time i autofill.

Click for image of problem...

thanks in advance.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 13 And Target = "Closed" Then
        Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
    End If
End Sub

Try this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    For Each cell In Target
        If cell.Column = 13 And cell = "Closed" Then
            Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
        End If
    Next cell
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM