excel - Dividing not adjacent cells using VBA -
any suggestions of how solve problem?
(updated)
numerator
,denominator
ranges containing integer values.
i want division cell cell of these ranges, e.g:
numerator(1)/denominator(1) numerator(2)/denominator(2) ...
and paste results in quotient
. ranges of same size , row-vector dimensioned.
my solution attempt:
sub divranges() dim targetrng range, sourcerng1 range, sourcerng2 range = 1 targetrng.cells.count quotient.cells(i).value = numerator.cells(i).value / denominator.cells(i).value next end sub
the problem divranges
doesn't loop through each value of ranges since row-vectors. instead goes horisontally 1 step down grows. problem occurs when cells of ranges not adjacent.
here's display of problem based on code mehow . result cells selected in sheet.
if spreadsheet looks
then code
sub divranges() dim quotient range, numerator range, denominator range set numerator = range("a1:d1") set denominator = range("a2:d2") set quotient = range("a3:d3") dim long = 1 quotient.count quotient.cells(i).value = numerator.cells(i).value / denominator.cells(i).value next end sub
gives you
update
not adjacent cells solution
assuming sheet
using below code
sub divranges() dim quotient range, numerator range, denominator range set numerator = range("a1, c1, d1, e1") set denominator = range("a2, c2, d2, e2") set quotient = range("a3, c3, d3, e3") dim c range dim long = 0 each c in quotient range(c.address) = range(getnum(numerator.address, i)) / range(getnum(denominator.address, i)) = + 1 next end sub function getnum(str string, long) string getnum = split(str, ",")(i) end function
result is
Comments
Post a Comment