首页 > 解决方案 > 将 2 个范围相乘

问题描述

我在该行中遇到语法错误

Range("E4:E" & ColALastRow) =Application.WorksheetFunction.Product=(Range("A4:A" & ColALastRow), Range("D4:D" & LastRowOfD))

Col A 和 Col D 有值。我想将它们相乘到 col E。下面是我到目前为止的代码。

    Dim rngData As Range
    Dim ColALastRow As Long, LastRowOfD As Long
    
    ColALastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set rngData = ActiveSheet.Range("A4:A" & ColALastRow)
    LastRowOfD = Cells(Rows.Count, "D").End(xlUp).Row

    Range("E4:E" & ColALastRow) =Application.WorksheetFunction.Product=(Range("A4:A" & ColALastRow), Range("D4:D" & LastRowOfD))

标签: excelvba

解决方案


您可以像下面这样简化您的方法并获得所需的结果。

Sub ProductofTwoCol()
    With Range("E1:E" & Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=A1*D1"
        .Value = .Value
    End With
End Sub

推荐阅读