Bug when inserting formula with VBA

blista99

New Member
Joined
Aug 28, 2015
Messages
10
Hy everyone

I have got this weird bug, which I can't understand.
With the following code in VBA I try to insert a formula:
Code:
    For Each Cell In Range("A28:A102")
        If Cell.Value <> "" Then
            Cell.Offset(0, 1).FormulaR1C1 = "=COUNTIF(Januar_Data!J:J,RC[-1])"
        End If
    Next

But the result in the cells is for example:
=COUNTIF(Januar_Data!J:(J),A28)

The "red" brackets just appear out of nowhere and corrupt the formula which then will not work.

Any ideas, what could be the problem?

Thanks for your suggestions.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You are mixing reference styles. Use:

"=COUNTIF(Januar_Data!C10,RC[-1])"

for the R1C1 version
 
Upvote 0
I don't see the difference? Do you mean I cannot use the "J:J"-Part? or
only write ....Formula="=COUNTIF(Januar_Data!J:J,RC[-1])" <- Endresult is same as above

Because if I type the formula manually into the cell, it works.
 
Upvote 0
You're mixing A1 style references (the J:J part) with R1C1 style (RC[-1]) and you can't do that - use one or the other. So either:

Rich (BB code):
Cell.Offset(0, 1).FormulaR1C1 = "=COUNTIF(Januar_Data!C10,RC[-1])"

(C10 = column 10, which is column J)

or use:

Rich (BB code):
Cell.Offset(0, 1).Formula = "=COUNTIF(Januar_Data!J:J," & cell.address(0, 0) & ")"
 
Upvote 0
:eek:
Wow. I feel stupid. I think I was into VBA for too long today....... C10...of course Collumn10. :whistle: I first thought about the cell "C10".

Yeah. Now it works. Thanks a bunch RoryA.
 
Upvote 0

Forum statistics

Threads
1,216,744
Messages
6,132,470
Members
449,729
Latest member
davelevnt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top