4

I have entered these formula in the second row of the Pth column:

=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)

When I drag it to the third row of the Pth column, it gets like this:

 =(COUNTIF(A$1:A2,A3)=0)+(COUNTIF(B$1:B2,B3)=0)+(COUNTIF(F$1:F2,F3)=0)

This is what I do manually. How do I make it using VBA? I have tried in the way below.

cells(Count,"M").formula= "=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)"

But it's not working. It's not changing from

"=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)" 

to

"=(COUNTIF(A$1:A2,A3)=0)+(COUNTIF(B$1:B2,B3)=0)+(COUNTIF(F$1:F2,F3)=0)"

How do I insert the formula into the cell when the formula keeps changing with an increase in row?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
niko
  • 9,285
  • 27
  • 84
  • 131
  • You could copy/paste the formula directly in VBA; instead of setting the formula to a text value, copy it from a cell in which it's correct, and paste in the target cell. – Joubarc Aug 22 '11 at 08:30

2 Answers2

5

You can do this in one line:

range("P2").Copy Destination:=range("P3:P10")

No need for variables, loops, anything!

Gaijinhunter
  • 14,587
  • 4
  • 51
  • 57
2

As suggested by Joubarc

Cells(2, "P").Copy
For Row = 3 To 10
     Cells(Row, "P").Select
     ActiveSheet.Paste
 Next Row
grantnz
  • 7,322
  • 1
  • 31
  • 38