【发布时间】:2023-03-27 05:10:02
【问题描述】:
我想将 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
【问题讨论】:
-
您可以在数组中传递您的范围并使用本文中描述的方法:stackoverflow.com/a/22056347/11231520。执行将比每行的读/乘/写快得多。
标签: excel vba multiplication