函数如何编码

时间:2018-06-21 06:52:03

标签: vba excel-vba excel

如果函数则如何编码

这是excel中的等效公式

if(Concrete_Class =“ A”,volume * cementfactor,“”)

Sub multiply()
    Dim second As Range
    Dim first As Range, prodt As Long
    Dim third As Range
    Dim sht As Worksheet
    Dim LastRow As Long


    LastRow = Cells(Rows.Count, "C").End(xlUp).Row

    Set first = Range("volume")
    Set third = Range("Cement")
    Set second = Range("cementfactor")

    For i = 1 To LastRow
            third(i, 1) = first(i, 1) * second

    Next i

    Rows(LastRow).Select
    Selection.Delete Shift:=xlUp
    Range("A2").Select
End Sub

示例:

img1

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

Sub multiply()
    Dim second As Range
    Dim first As Range, prodt As Long
    Dim third As Range
    Dim sht As Worksheet
    Dim LastRow As Long


    LastRow = Cells(Rows.Count, "C").End(xlUp).Row

    Set first = Range("volume")
    Set third = Range("Cement")
    Set second = Range("cementfactor")


    For i = 1 To LastRow
        If Cells(i, 2).Value = "A" Then
            third(i, 1) = first(i, 1) * second
        End If
    Next i

    Rows(LastRow).Select
    Selection.Delete Shift:=xlUp
    Range("A2").Select
End Sub