Trying to divide two cells with a macro

C

cqmman

Hello,

Another simple problem I hope!

I have a list of rows (lets say 80), and I want column R to contain
the answer from the number in cell "l", divided from the number in
"'d" for each of the 80 columns. The values in columns L and D are
both numbers.


Based on a previously helpful bit of code, I thought I could do this:


Set col1 = Sheets("Historical").Columns("d")
Set col2 = Sheets("Historical").Columns("l")

For Each c In Range("r1:r80") 'unique list in sh1
c.Value = col2.Value / col1.Value

Next c

I have tried playing around and using things like:
c.value = col2 / col1

Or even this:

DivideBy(col2,col1)

Function DivideBy(ByVal Numerator As Double, ByVal Denominator As
Double) As Double
DivideBy = Numerator / Denominator

End Function


But still can't quite get it right. What am I doing wrong?

Cheers
 
A

AltaEgo

Sub AddCalcsToColD()
'I presume column D contains values
'while column R is blank
'therefore select column D first
Application.Goto Reference:="R65536C4"
Selection.End(xlUp).Select
Range(Selection, "D2").Select 'change D2
'to topmost cell required
'move the selection to column R
Selection.Offset(, 14).Select
' insert formula
Selection.FormulaR1C1 = "=RC[-9]/RC[-14]"

' if you want values rather than formula leave the following
'otherwise delete
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

End Sub
 
G

GMorris

Wouldn't be easier to just use a formula or even an
array fomula? Like "{=L1:L80/D1:D80}
(That's with Ctlr-Shift Enter after selecting the R1:R80 range.)
Or am I missing something here?
Otherwise, unless you NEED to do it in code, why bother...
Just curious.
 
D

Don Guillett

Why not just use

mc = "r"
for i=1 to cells(rows.count,mc).end(xlup).row
cells(i,"r").value=cells(i,"L")/cells(i,"d")
next
 
C

cqmman

Why not just use

mc = "r"
for i=1 to cells(rows.count,mc).end(xlup).row
cells(i,"r").value=cells(i,"L")/cells(i,"d")
next

Great, thanks again Don, and everyone else!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top