Formula not working when entered by VBA

fali34533

New Member
Joined
Apr 5, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
' finds the last row of column F
LastRow2 = Range("F" & Rows.Count).End(xlUp).Address

'this is the formula I'm trying to enter
ActiveCell.Formula = "=SUMPRODUCT(COUNTIFS(F2:F" & LastRow2 & ",RC[-1]))"

It does go into the cell need by appears with extra marks around the cell reference
=SUMPRODUCT(COUNTIFS('F2':'F6',E8))

Any help will be much apricated.

Thank you
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi & welcome to MrExcel.
You are mixing A1 & R1C1 notation which is the problem. What cell is the formula going into?
 
Upvote 0
Hi @fali34533. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

I have some comments for you:
SUMPRODUCT(COUNTIFS(F2:F" & LastRow2 & ",RC[-1]))
1. You are using 2 different types of cell naming.

2. It should be something like this:
VBA Code:
Sub enterformula()
  Dim LastRow2 As Long
 
  LastRow2 = Range("F" & Rows.Count).End(xlUp).Row
  ActiveCell.FormulaR1C1 = "=SUMPRODUCT(COUNTIFS(R2C6:R" & LastRow2 & "C6" & ",RC[-1]))"
End Sub

3. You should use .Row to get the row and not . Address.
LastRow2 = Range("F" & Rows.Count).End(xlUp).Address

Rich (BB code):
LastRow2 = Range("F" & Rows.Count).End(xlUp).Row

4. Your formula does not need sumproduct function. Then:
VBA Code:
Sub enterformula()
  Dim LastRow2 As Long
 
  LastRow2 = Range("F" & Rows.Count).End(xlUp).Row
  ActiveCell.FormulaR1C1 = "=COUNTIFS(R2C6:R" & LastRow2 & "C6" & ",RC[-1])"
End Sub
--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
A few things...

1) Return the row number instead of the address to LastRow2 so that you can use it in your formula to specify the row number.

2) Use either the A1 reference style or the R1C1 reference style, but not both.

3) Use ActiveCell.FormulaR1C1 instead of ActiveCell.Formula, if using the R1C1 reference style.

4) Since the criteria contains only a single value, not an array, you can eliminate SUMPRODUCT.

So, for example, if you are going to use the R1C1 reference style, try...

VBA Code:
' finds the last row of column F
LastRow2 = Range("F" & Rows.Count).End(xlUp).Row

ActiveCell.FormulaR1C1 = "=COUNTIFS(R2C6:R" & LastRow2 & "C6,RC[-1])"

Hope this helps!
 
Upvote 0
Hi & welcome to MrExcel.
You are mixing A1 & R1C1 notation which is the problem. What cell is the formula going into?

The formula is going into Cell F4.

How do I reference column "F" which holds the data with using A1 notation?
 
Upvote 0
Hi @fali34533. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

I have some comments for you:
SUMPRODUCT(COUNTIFS(F2:F" & LastRow2 & ",RC[-1]))
1. You are using 2 different types of cell naming.

2. It should be something like this:
VBA Code:
Sub enterformula()
  Dim LastRow2 As Long
 
  LastRow2 = Range("F" & Rows.Count).End(xlUp).Row
  ActiveCell.FormulaR1C1 = "=SUMPRODUCT(COUNTIFS(R2C6:R" & LastRow2 & "C6" & ",RC[-1]))"
End Sub

3. You should use .Row to get the row and not . Address.
LastRow2 = Range("F" & Rows.Count).End(xlUp).Address

Rich (BB code):
LastRow2 = Range("F" & Rows.Count).End(xlUp).Row

4. Your formula does not need sumproduct function. Then:
VBA Code:
Sub enterformula()
  Dim LastRow2 As Long
 
  LastRow2 = Range("F" & Rows.Count).End(xlUp).Row
  ActiveCell.FormulaR1C1 = "=COUNTIFS(R2C6:R" & LastRow2 & "C6" & ",RC[-1])"
End Sub
--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Thank you Dante, that has solved the problem

I really apricate the help
 
Upvote 1
I am putting the formula in cell G4 and it counts the values in column F.

For example:
varios 05abr2023.xlsm
FG
1
2d
3c
4d3
5e
6d
7g
KB123 Y
Cell Formulas
RangeFormula
G4G4=COUNTIFS(F2:F7,F4)

Then:
VBA Code:
Sub enterformula()
  Dim LastRow2 As Long
  
  LastRow2 = Range("F" & Rows.Count).End(xlUp).Row
  Range("G4").Formula = "=COUNTIFS(F2:F" & LastRow2 & ",F4)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,139
Members
449,098
Latest member
Doanvanhieu

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