Using VBA to put a COUNTA formula in a cell

zookeepertx

Well-known Member
Joined
May 27, 2011
Messages
586
Office Version
  1. 365
Platform
  1. Windows
Hello again!
I'm trying to put a formula into a cell that reads like the following example: =COUNTA(C10"C12) but it simply refuses to do so.
The range of cells to be referenced varies, so in my code I've tried to address that but keep getting a "Compile error: Expected: end of statement". Can someone tell me what's wrong? It seems like it should be pretty easy since I don't want to use the COUNTA as part of the actual code, I just want to enter the formula into a cell!
I get the error after I type the code, not when I try to run it. I can't even get to the running it part!
The error shows up at the "B" in this line: Range("J" & i).Formula = "=COUNTA("B" & r & ":" & "B" & i - 1)" in the below example of code

VBA Code:
r = 6
For i = 6 To lr Step 1
    If Range("B" & i).Value = "Total" Then
        Range("B" & i).Formula = "=COUNTA("B" & r & ":" & "B" & i - 1)"
        Rows(i + 1).Insert Shift:=xlDown
        Range("C" & i & ":" & "I" & i).ClearContents
    End If
r = i + 1
Next i

I thought maybe it wanted me to make it a separate For/Next, so then I tried this, but no luck.

VBA Code:
r = 6
For i = 6 To lr Step 1
    If Range("B" & i).Value = "Total" Then
        Range("B" & i).Select
        Selection.Formula = "=COUNTA("B" & r & ":" & "B" & i - 1)"
    End If
r = i + 1
Next i

r = 6
For i = 6 To lr Step 1
    If Range("C" & i).Value = "Total" Then
        Rows(i + 1).Insert Shift:=xlDown
        Range("C" & i & ":" & "I" & i).ClearContents
    End If
r = i + 1
Next i

I'm feeling like an idiot because I should be able to figure this out but frankly, I'm just sick of looking at it. I've spent way too much time on it already today and I just need some help, PLEASE!

Thanks!

Jenny
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Can you show us what your expected result is?
Give us the first 3 cases, i.e.
- The address of the first cell you want this formula in, and the exact formula in that cell
- The address of the second cell you want this formula in, and the exact formula in that cell
- The address of the third cell you want this formula in, and the exact formula in that cell

Once I see this pattern, I think I should be able to come up with the code you want.
 
Upvote 0
The message means that something is not correctly delimited. Perhaps you want
Range("B" & i).Formula = "=COUNTA(""B"" & r & "":B"" & i -1)"""

or using single quotes instead of delimited double quotes
Range("B" & i).Formula = "=COUNTA('B' & r & ':B' & i -1)"""

Note that it is not necessary to separate your ":"
 
Upvote 0
Hi Joe,

I'll have to post screenshots since this is my work computer and I can't use the hosting websites suggested in the rules.
This is what the sheet looks like after I run the part of my macro that works so far.
Frt1.jpg


Then I've got code that clears all instances of the word "Total". (which does work) Then, under "Shipment Count" I need to put a formula to count the cells in B that have data for that Country. I also need to put a formula J-M of the same row to sum the data for that country. (I tried to do that the same way as the COUNTA, thinking SUM might be easier, but had the same problem.)

Frt3.jpg


Thanks for the help!
 
Upvote 0
The message means that something is not correctly delimited. Perhaps you want
Range("B" & i).Formula = "=COUNTA(""B"" & r & "":B"" & i -1)"""

or using single quotes instead of delimited double quotes
Range("B" & i).Formula = "=COUNTA('B' & r & ':B' & i -1)"""

Note that it is not necessary to separate your ":"

Thanks for the answer, but it doesn't like either one of those. :(
 
Upvote 0
OK, sorry it didn't work. Your posted version did cause a compile error (syntax) for me but neither of the ones I posted did.
 
Upvote 0
Hmm, maybe
"=COUNTA(B" & r & ":B" & i - 1 & ")"

May not be the exact result you want in your sheet but syntax seems to work for me.
1674759535340.png
 
Upvote 0
Hmm, maybe
"=COUNTA(B" & r & ":B" & i - 1 & ")"

May not be the exact result you want in your sheet but syntax seems to work for me.
View attachment 83839

Hmmm, still doesn't work over here, but you did remind me that I need to combine the colon with the second B without the extra quotation marks and the "&", so that makes it at least LOOK better. But I still get the error. (Maybe I'm just not holding my mouth right, LOL!)
 
Upvote 0
Did I just discover that the line you say raises an error is not in any of the code you posted? Where is
Range("J" & i)
 
Upvote 0
Did I just discover that the line you say raises an error is not in any of the code you posted? Where is
Oh, sorry, That was when I was trying to put a SUM in column J to sum the data for that country. I thought maybe if I could get THAT to work, it might give me a clue to the COUNTA but it didn't work. But I just realized that that line of code is totally wrong. However, the line
VBA Code:
Selection.Formula = "=COUNTA("B" & r & ":B" & i - 1)"
is still where the problem lies as it keeps giving the error message on that line, with the first B highlighted, whether it's the first bit of code or the second. And the error still comes up when I type the line and won't let me even try to run the macro.

Meanwhile, I'm about to clock out for the day, so I'll be checking back here tomorrow.
Thanks for being patient with me.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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