2

我怀疑这并不是那么复杂,但我没有太多运气找到合适的谷歌条款......所以我来找专家!

所以我正在尝试实现一个Worksheet_Change事件。这非常简单,我基本上只想做以下事情:

如果 C 列中的值发生变化,并且 D 中的值(在该行中)具有特定格式(NumberFormat = "$ 0.00"),则 E 列(在该行中)是这两个值的乘积。简单的。实际上,我只想要在 E 列中使用公式的 VBA 等效项。这是我正在使用的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 And Target.Value <> "" Then
    If Target.Offset(0, 1).NumberFormat = "$ 0.00" Then
        Target.Offset(0, 2).Value = Target.Value * Target.Offset(0, 1).Value
        End If
        End If   
end sub        

当我尝试将多个值粘贴到 c 列的多行中时,我的问题出现了。即,我将一列数据(> 1 行)复制到 C 中,但出现类型不匹配错误。我将做出巨大的飞跃,因为它不能很好地处理这个问题,因为“目标”旨在成为单个单元格而不是一个组。我希望有一种简单的方法来处理这个问题,每次工作表上的单元格或其他东西发生变化时都不会涉及一些疯狂的循环。

提前致谢!

4

2 回答 2

2

这是你正在尝试的吗?

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim aCell As Range

    On Error GoTo Whoa

    Application.EnableEvents = False

    If Not Intersect(Target, Columns(3)) Is Nothing Then
        For Each aCell In Target
            If aCell.Value <> "" And aCell.Offset(0, 1).NumberFormat = "$ 0.00" Then
                aCell.Offset(0, 2).Value = aCell.Value * aCell.Offset(0, 1).Value
            End If
        Next
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

您可能还想阅读这个

虽然您只想捕获 Col C 粘贴,但这里还有另一种情况,用户在多列中粘贴(其中一个是 Col C)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim aCell As Range

    On Error GoTo Whoa

    Application.EnableEvents = False


    If Not Intersect(Target, Columns(3)) Is Nothing Then
        If Not Target.Columns.Count > 1 Then
            For Each aCell In Target
                If aCell.Value <> "" And aCell.Offset(0, 1).NumberFormat = "$ 0.00" Then
                    aCell.Offset(0, 2).Value = aCell.Value * aCell.Offset(0, 1).Value
                End If
            Next
        Else
            MsgBox "Please paste in 1 Column"
        End If
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
于 2013-04-21T17:49:01.413 回答
0

本着完整性和协作的精神,我在这里发布了 Siddharth Rout 方法的变体;不同之处在于,这不依赖于“要作用的单元格”都在一个列中。这使它更干净一些,并且更容易适应其他场景。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim aCell As Range
    Dim onlyThese As Range   ' collection of ranges that, if changed, trigger some action
    Dim cellsToUse As Range  ' cells that are both in "Target" and in "onlyThese"

    On Error GoTo Whoa

    Application.EnableEvents = False

    Set onlyThese = Range("C:C") ' in this instance, but could be anything - even a union of ranges
    Set cellsToUse = Intersect(onlyThese, Target)
    If cellsToUse Is Nothing Then GoTo Letscontinue

    ' loop over cells that were changed, and of interest:
    For Each aCell In cellsToUse
        If aCell.Value <> "" And aCell.Offset(0, 1).NumberFormat = "$ 0.00" Then
            aCell.Offset(0, 2).Value = aCell.Value * aCell.Offset(0, 1).Value
        End If
    Next

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
于 2013-04-21T21:20:49.090 回答