当我在“单元格A2等”中输入数据时,自动填写并执行“单元格C2等”的公式

时间:2019-07-10 17:21:12

标签: excel vba autofill

当我使用VBA将数据输入到单元格(A:A)减去标题中时,我想自动填充单元格(C:C)减去标题。对于我要自动填充到单元格C:C中的公式,逻辑上是对/错,但是将其替换为扫描仪1和2。

=IF(ISNUMBER(SEARCH("$",$A2)),"Scanner 2",IF(ISNUMBER(SEARCH("#",$A2)),"Scanner 1","Error"))

下面是我尝试的VBA代码,由于我是VBA的新手,它是否可能是错误的:(也许使用函数而不是private子程序?)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rINT As Range
Dim rCell As Range
Dim tCell As Range

Set rINT = Intersect(Target, Range("A:A"))
If Not rINT Is Nothing Then
    For Each rCell In rINT
        Set tCell = rCell.Offset(0, 2)
        If IsEmpty(tCell) Then
            With ThisWorkbook.Sheets("BOD_Barcode")
            tCell = tCell.Formula = "=IF(ISNUMBER(SEARCH(""$"",$A2)),""Scanner 2"",IF(ISNUMBER(SEARCH(""#"",$A2)),""Scanner 1"",""Error""))"
            End With
        End If
    Next
End If
End Sub 

1 个答案:

答案 0 :(得分:1)

我没有直接使用VBA,而是直接向单元格使用公式,并启用了选项设置中的迭代。因此,当您输入数据时,它将检查是否有数据-在我的情况下,它将运行公式,但是您可以将其用于时间戳或任何函数。

=IF($A2<>"",IF($C2="",IF(ISNUMBER(SEARCH("$",$A2)),"Scanner 2", IF(ISNUMBER(SEARCH("#",$A2)),"Scanner 1","Error")),$C2),"")