vba 范围乘以范围

时间:2021-06-23 10:54:51

标签: excel vba multiplication

我想将 P 列的单元格与 M 列的单元格相乘,并用相应的乘积替换 P 列的内容。之后我想对 Q 和 N 列做同样的事情。

我一直在尝试查找此问题,最接近的解决方案是:VBA multiply two named ranges

不幸的是,在运行第一列并计算它之后,Excel 给了我一个运行时错误 13 - 类型不匹配。

我的代码:

Sub rechnen_mod()

Dim aud_y As Range
Dim soc_r As Range
Dim mp_y As Range
Dim mp_r As Range

Set aud_y = Sheets("MRP score template").[P11:P1000]
Set soc_r = Sheets("MRP score template").[Q11:Q1000]
Set mp_y = Sheets("MRP score template").[M11:M1000]
Set mp_r = Sheets("MRP score template").[N11:N1000]

For i = 1 To Range("P").End(xlDown).Row
    aud_y(i, 1) = aud_y(i, 1) * mp_y(i, 1)
Next i

For j = 1 To Range("Q").End(xlDown).Row
    soc_r(j, 1) = soc_r(j, 1) * mp_r(j, 1)
Next j

End Sub

任何帮助将不胜感激。

编辑:阅读 后,我将代码更改为:

Public Sub array1()

Dim x As Long
Dim arr
Dim arr_e
Dim arrf
Dim arrf_e
Dim results
Dim r As Range

arr = Sheets("MRP score template").[P11:P473]
arrf = Sheets("MRP score template").[M11:M473]

ReDim results(1 To UBound(arr) * UBound(arrf))

For Each arr_e In arr
    For Each arrf_e In arrf
        x = x + 1
        results(x) = arr_e * arrf_e
    Next arrf_e
Next arr_e

Set r = Sheets("calc").Range("A1:A" & UBound(results))

r = Application.Transpose(results)

End Sub

Excel 给我一个运行时错误 13 - 类型不匹配,并解释为 arrf_e = 错误 2402。经过快速研究,这应该意味着该数组包含 #NA - 但它没有。

点击调试后,标记的那一行是

<块引用>

结果(x) = arr_e * arrf_e

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码。我还添加了注释来解释每个步骤:)

<script>
function toggleNav() {
    var sidenav = document.getElementById("mySidenav"),
    main = document.getElementById("main");
    sidenav.style.width = sidenav.style.width === "250px" ? '0' : '250px';
    wrapper.style.marginLeft = wrapper.style.marginLeft === "250px" ? 'auto' :  '250px';
}
</script>

<script>
function myFunction(x) {
  x.classList.toggle("fas fa-times");
}
</script>

我用 250 行的随机值对其进行了测试,效果很好。