如果下一列在该行中有数据,则将公式添加到列中的VBA脚本

时间:2014-07-17 19:50:02

标签: excel-vba excel-formula excel-2010 vba excel

我一直在研究一个项目,我在excel中有一个嵌套IF(countif(),,*)公式,以帮助我对CSV文件进行分类。

我正在尝试这样做,当导入CSV时(到单元格B2),A列将填充公式。我很难将配方复制到1个单元格中,因为无论我怎么努力,它都不适合在一条线上。公式是检查3个不同细胞的内容,可能有23个以上的输出给你一个想法。

无论如何:

  1. 获取一个VBA脚本来复制公式(我已经记录了一个 宏甚至未经修改的宏都破了)
  2. 使宏只用尽可能多的行填充 OR
  3. 在VBA中执行整个计算并相应地填充单元格。
  4. 以下是我在每行中使用的公式,其中CSV文件中可能有几千个。

    =IF(COUNTIF(BZ2,"8075 *research*"),
    IF(COUNTIF(BR2, "780 B*"),
        IF(COUNTIF(AU2,"*ground*"),"51",
            IF(COUNTIF(AU2,"*next*"),"52",
                IF(COUNTIF(AU2,"*2*"),"53",
                    IF(COUNTIF(AU2,"*3*"),"54",
                        IF(COUNTIF(AU2,"*charge*"),"546","Unclassified"
                        )
                    )
    
                )
            )
        ),
        IF(COUNTIF(AU2,"*ground*"),"25430",
            IF(COUNTIF(AU2,"*next*"),"25431",
                IF(COUNTIF(AU2,"*2*"),"25432",
                    IF(COUNTIF(AU2,"*3*"),"25433",
                        IF(COUNTIF(AU2,"*charge*"),"2546","Unclassified"
                        )
                    )
                )
            )
        )
    ), 
    IF(COUNTIF(BZ2, "780 B*"),
        IF(COUNTIF(BR2,"8075 *research*"),
            IF(COUNTIF(AU2,"*ground*"),"251",
                IF(COUNTIF(AU2,"*next*"),"252",
                    IF(COUNTIF(AU2,"*2*"),"253",
                        IF(COUNTIF(AU2,"*3*"),"254",
                            IF(COUNTIF(AU2,"*charge*"),"2546","Unclassified"
                            )
                        )
                    )
                )
            ),
            IF(COUNTIF(AU2,"*ground*"),"15430",
                IF(COUNTIF(AU2,"*next*"),"15431",
                    IF(COUNTIF(AU2,"*2*"),"15432",
                        IF(COUNTIF(AU2,"*3*"),"15433",
                            IF(COUNTIF(AU2,"*charge*"),"1546","Unclassified"
                            )
                        )
                    )
                )
            )
    
        ),
        IF(COUNTIF(BR2, "780 B*"),"540",
            IF(COUNTIF(BR2,"8075 *research*"),"2540", "Unclassified"
            )
        )   
    )
    

1 个答案:

答案 0 :(得分:1)

好的,我没试过这个,但我认为你应该沿着以下几行写一个宏或UDF:

    Public Sub CheckMyCells()

    Dim thisCell As Range

    With ThisWorkbook.Sheets("MySheet")
        For Each thisCell In .Range("RangeWithCSVValues")
            If .Range("BZ2").Value2 = "8075 *research*" Then
                If .Range("BR2").Value2 = "780 B*" Then
                    Select Case .Range("AU2").Value2
                        Case "*ground*"
                            thisCell.Value = 51
                        Case "*next*"
                            thisCell.Value = 51
                        Case "*2*"
                            thisCell.Value = 53
                         Case "*3*"
                            thisCell.Value = 54
                        Case "*charge*"
                            thisCell.Value = 546
                        Case Else
                            thisCell.Value = "ERROR!"
                Else
                    Case "*ground*"
                            thisCell.Value = 25430
                        Case "*next*"
                            thisCell.Value = 25431
                        Case "*2*"
                            thisCell.Value = 25432
                         Case "*3*"
                            thisCell.Value = 25433
                        Case "*charge*"
                            thisCell.Value = 2546
                        Case Else
                            thisCell.Value = "ERROR!"
                Else
                    '... Same thing again but with different values ...
                End If
            End If
        Next
    End With

End Sub

导入数据后执行该操作。 显然,MySheet是您工作的工作表的名称,RangeWithCSVValues是包含您要处理的值的范围的名称,例如"A1:A2300"

希望这能让你开始。