VBA array multiplication, individually without looping
Greetings to you: Follow the line:
Option Base 1
Sub Test()
Dim aa() As Integer
Dim bb() As Integer
ReDim aa(3)
ReDim bb(3)
For j = 1 To 3
aa(j) = j * 2
bb(j) = j * 3
Next j
End Sub
Now, the only little thing I want to do is to multiply the two one-dimensional arrays on elements without loops, and then dump this new array (6,24,54) in the range. I'm sure it should be easy. The solution I would see is to create a diagonal matrix (array) and then use mmult, but I'm sure it's very easy to do this. Thanks for the help.
source to share
It is not possible to perform multiplication on every element of an array without a loop. Some languages ββhave methods that seem to do just that, but they get stuck under the hood.
As you mentioned in your comments, you have 2 options:
- Scroll through the range and multiply
- Cast a range to an array, do a multiplication, then cast back to a range
It all depends on your data, but you'll almost always find that dumping a range into a variant array, doing your job, and dumping it back will be much faster than looping through a series of cells. Keep in mind that you reset it back to range will also affect the speed.
source to share