gpt4 book ai didi

arrays - 将2个数组乘以第三个空白数组范围VBA(Excel)

转载 作者:行者123 更新时间:2023-12-04 22:29:19 26 4
gpt4 key购买 nike

Dim i As Integer, q As Integer
Dim rng As Range
Dim my_array1elm
Dim my_array2elm
Dim x As Long

Sub Yoo()

Range("B1").Select
For i = 1 To 12
ActiveCell.Value = i
ActiveCell.Offset(0, 1).Select
Next

Range("A2").Select
For q = 1 To 12
ActiveCell.Value = q
ActiveCell.Offset(1, 0).Select
Next

my_array1 = Range("B1:M1").Select
my_array2 = Range("A2:A13").Select
my_array3 = Range("B2:M13").Select

现在我想将 my_array1 中的每个元素与 my_array2 中的每个元素相乘。然后将结果(矩阵)填充到 my_array3

很难弄清楚循环。

最佳答案

读写工作表的效率很低。最好在 VBA 数组中进行乘法运算,然后将数组写入工作表。

请注意,水平阵列是一维的,垂直和多列阵列是二维的。

例如:

Option Explicit

Dim i As Integer, q As Integer
Dim rng As Range
Dim my_array1
Dim my_array2
Dim my_array3

Sub Yoo()

ReDim my_array1(1 To 12) 'horizontal array
ReDim my_array2(1 To 12, 1 To 1) 'vertical array
For i = 1 To 12
my_array1(i) = i
my_array2(i, 1) = i
Next i

ReDim my_array3(1 To 12, 1 To 12) 'results array
For i = 1 To 12
For q = 1 To 12
my_array3(i, q) = my_array1(i) * my_array2(q, 1)
Next q
Next i

Cells.Clear
Range("B1:M1") = my_array1
Range("A2:A13") = my_array2
Range("B2:M13") = my_array3

End Sub

enter image description here

关于arrays - 将2个数组乘以第三个空白数组范围VBA(Excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54612207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com