Divide one range with another

  • Thread starter Thread starter Josip
  • Start date Start date
J

Josip

Hello,

is there a way in VBA to perform elementwise division on two ranges of
numbers, say A1:B10 and C1:D10? I.e.
A1/C1, A2/C2, ...

Thanks,
J
 
Josip,

Yes tha's possible but your question isn't very clear. What are the ranges
and how do you want the answers produced to be presented. This divides column
A by column C and prints the answer to the immediate window

On Error Resume Next
For x = 1 To 10
Debug.Print Cells(x, 1) / Cells(x, 3)
Next
End Sub

Mike
 
Thank you,
sorry if I was a bit unclear. So, the problem is that I have two sets
of ranges, e.g.:
1. Columns A and B, rows 1 to 10.
2. Columns C and D, rows 1 to 10.

And I want to store this data in a Range variable for later use. Would
it be possible to use something like this:
"Set myRange = Cells(1, 1).Resize(2, 10) / Cells(3, 1).Resize(2, 10)"

Ok, that does not work, but something like that? Or do I need to use
those fore claues?
 
Hi,

Try this

For Each c In Range("A1:a10")
c.Offset(, 4).Value = (c + c.Offset(, 1)) / (c.Offset(, 2) + c.Offset(, 3))
Next

Mike
 
Thank you very much,

c.Offset(, 4).Value = c / c.Offset(, 2)

worked perfectly for my needs.
 
Actuall, one more thing. Is it possible to store these results in a
Range variable and not justi in cells determined by the offset(,4)?
 
Unless you need the result in VBA for other reasons

=SUM(A1:B10/C1:D10)

Array enter with Ctrl-Shift-Enter

Regards,
Peter T
 
Hi,

You can put them in an array. The message box bit at the end is just to
demonstrate it worked.

Sub Populate_an_Array()
x = 1
Dim vaArray() As Variant
lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
ReDim vaArray(1 To lastrow)
For Each c In Range("A1:A" & lastrow)
vaArray(x) = c / c.Offset(, 2)
x = x + 1
Next

For x = 1 To 10
MsgBox vaArray(x)
Next
End Sub

Mike
 
Back
Top