Worksheet_Change多个更改

huangapple go评论77阅读模式
英文:

Worksheet_Change multiple changes

问题

以下是翻译好的部分:

我有以下问题。如果我删除多个单元格或对多个单元格执行选择编辑操作,我会收到错误消息。我的代码如下;

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Value <> PreviousValue Then
    Sheets("log").Cells(80000, 1).End(xlUp).Offset(1, 0).Value = _
        Application.UserName & " has changed cell " & Target.Address _
        & " from " & PreviousValue & " to " & Target.Value & 
    End If

End Sub

我做错了什么?

英文:

I have the following question. I get an error if I delete multiple cells or perform a selection edit on multiple cells. My code is as follows;

    Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Value &lt;&gt; PreviousValue Then
    Sheets(&quot;log&quot;).Cells(80000, 1).End(xlUp).Offset(1, 0).Value = _
        Application.UserName &amp; &quot; heeft cel &quot; &amp; Target.Address _
        &amp; &quot; from &quot; &amp; PreviousValue &amp; &quot; to &quot; &amp; Target.Value &amp; 
    End If

End Sub

What am I doing wrong?

答案1

得分: 2

如果Target是一个多单元格范围,那么比较Target.Value <> PreviousValue将失败(Target.Value然后是一个二维数组)。

使用循环来比较每个单独的单元格:

Dim rng as Range
For Each rng in Target
     If rng.Value <> PreviousValue Then
英文:

If Target is a multi-cell range, then the comparison Target.Value &lt;&gt; PreviousValue will fail (Target.Value is then a 2D array).

Use a loop to compare each individual cell:

Dim rng as Range
For Each rng in Target
     If rng.Value &lt;&gt; PreviousValue Then

huangapple
  • 本文由 发表于 2020年1月6日 21:36:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定