View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
SAMURA SAMURA is offline
external usenet poster
 
Posts: 8
Default Excel VBA - loop code problem

Hi. I fixed the two points.


Sub Macro2()

Dim n As Integer

Range("D1").Select

n = 1

Do

'needed to write the R1C1 formula.
ActiveCell.FormulaR1C1 = "=Average(RC[-3], RC[-2])"

'the ActiceCell shifts under the current cell
ActiveCell.Offset(1, 0).Select

n = n + 1

Loop Until IsEmpty(Cells(n, 1))

End Sub


--
Samura


"ellis_x " wrote in message
...
Hello,

I am a newb at VBA programming, but I am trying to get a loop code to
run. A simple example in below.


Code:
--------------------

Sub Macro2()

' This loop runs until there is nothing in the next column

Range("D1").Select

Dim n As Variant

n = 1
Do
ActiveCell.FormulaR1C1 = "=Average(Cells(n, 1), Cells(n, 2))"
ActiveCell.Offset(0, 2).Select
n = n + 1
Loop Until IsEmpty(Cells(n, 1))

End Sub

--------------------


Hopefully someone here will be able to spot what is wrong with what
I've done here and be kind enough to help.

What is posted here, just puts "=Average(Cells(n, 1), Cells(n, 2))"
iin each of the cells - instead of calculating the number .

Using

Code:
--------------------

ActiveCell.FormulaR1C1 = "=Average(Cells(n, 1).Value, Cells(n,

2).Value)"

--------------------

comes back with an error.