adding two ranges
Monique,
What you are doing wrong:
#1 It's a function, which can't change the value of cells - a function can only return a value to
the cell from which it is called.
#2 sCell is always the last cell in RngTwo, because of your for each statement prior to the adding
Perhaps something like this:
Sub AddCells(RngOne As Range, RngTwo As Range)
Dim i As Integer
If RngOne.Cells.Count < RngTwo.Cells.Count Then
MsgBox "The ranges are not similarly sized"
Exit Sub
End If
For i = 1 To RngOne.Cells.Count
RngOne.Cells(i).Value = RngOne.Cells(i).Value + _
RngTwo.Cells(i).Value
Next i
End Sub
'Called like
Sub test()
AddCells Range("A1:A10"), Range("B1:B10")
End Sub
HTH,
Bernie
MS Excel MVP
"Monique" wrote in message
...
Below is a function that I am creating that will add the cells in each range
with each other. It does not work. Can anyone tell me what it is I'm doing
wrong?
Thanks in advance
Public Function AddCells(RngOne As Range, RngTwo As Range)
Dim fCell As Range
Dim sCell As Range
For Each fCell In RngOne
For Each sCell In RngTwo
sCell.Value = sCell
Next sCell
fCell.Value = fCell + sCell
Next fCell
End Function
|