Excel VBA - Adopt formula to ActiveCell

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Hi,

I intend to trigger from Personal Macro Workbook by a custom button in QAT.

Act upon an ActiveCell.

Active cell is going to be in a Header Row of a spreadsheet. Header Row can be in any row, not necessarily in row one. ActiveCell in any column.

Need VBA to adopt the below formula, such that:
  1. "XX" in it would be replaced by the existing content of the ActiveCell
  2. "A4" in it would be replaced by the Column letter of the ActiveCell and a row number below the ActiveCell
  3. Letter in "A1000000" in it would be replaced by the Column letter of the ActiveCell
*Additional caveat is that Excel does not allow easily to enter a formula into a header, but that's secondary...

Excel Formula:
="XX. Tot: " & COUNTA(A4:A1000000) & " Vis: " & AGGREGATE(3,3,A4:A1000000)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

I intend to trigger from Personal Macro Workbook by a custom button in QAT.

Act upon an ActiveCell.

Active cell is going to be in a Header Row of a spreadsheet. Header Row can be in any row, not necessarily in row one. ActiveCell in any column.

Need VBA to adopt the below formula, such that:
  1. "XX" in it would be replaced by the existing content of the ActiveCell
  2. "A4" in it would be replaced by the Column letter of the ActiveCell and a row number below the ActiveCell
  3. Letter in "A1000000" in it would be replaced by the Column letter of the ActiveCell
*Additional caveat is that Excel does not allow easily to enter a formula into a header, but that's secondary...

Excel Formula:
="XX. Tot: " & COUNTA(A4:A1000000) & " Vis: " & AGGREGATE(3,3,A4:A1000000)

So far I got the code below, but get following error on line:

VBA Code:
activeCell.formula = formula

2022-12-19_07h28_19.png


Code:
Sub ReplaceFormula()
    'Declare variables
    Dim aCell As Range
    Dim columnLetter As String
    Dim headerValue As String
    Dim formula As String
  
    'Get the active cell and its column letter
    Set aCell = activeCell
    columnLetter = Split(Cells(1, activeCell.Column).Address, "$")(1)
  
    'Get the value of the active cell (i.e., the header value)
    headerValue = activeCell.Value
  
    'Construct the formula using the active cell's value and column letter
    formula = "= """ & headerValue & """. Tot: " & "COUNTA(" & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)" & " Vis: " & "AGGREGATE(3,3," & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)"
  
    'Insert the formula into the active cell
    activeCell.formula = formula
End Sub
 
Upvote 0
NEVER, NEVER use reserved words, like "formula", as names of variables (or names of procedures)!
It can cause errors and unexpected results, as your code so clearly demonstrates.

Just look at this line here:
VBA Code:
activeCell.formula = formula
You want the first "formula" to be the "formula" property, but the second one to be your "formula" variable.
How can VBA figure out which one you mean in each case? The ambiguity and uncertainty can lead to unexpected results amd errors!

Therefore, you should NEVER use reserved words (words of existing properties, methods, functions, events, etc) as names of your variables (or procedures or user-defined functions).
I often preface my variables with the "my" prefix to prevent this from ever happening, i.e. "myformula".
 
Upvote 0
NEVER, NEVER use reserved words, like "formula", as names of variables (or names of procedures)!
It can cause errors and unexpected results, as your code so clearly demonstrates.

Just look at this line here:
VBA Code:
activeCell.formula = formula
You want the first "formula" to be the "formula" property, but the second one to be your "formula" variable.
How can VBA figure out which one you mean in each case? The ambiguity and uncertainty can lead to unexpected results amd errors!

Therefore, you should NEVER use reserved words (words of existing properties, methods, functions, events, etc) as names of your variables (or procedures or user-defined functions).
I often preface my variables with the "my" prefix to prevent this from ever happening, i.e. "myformula".
That makes sense. I got the code by feeding my original post to ChatGPT, so blame Ai for that 😜
I made the correction per your note, but getting the same error on the same line... And even when I changed that variable to "myfmula", same result...

VBA Code:
Sub ReplaceFormula()
    'Declare variables
    Dim aCell As Range
    Dim columnLetter As String
    Dim headerValue As String
    Dim myfmula As String
  
    'Get the active cell and its column letter
    Set aCell = activeCell
    columnLetter = Split(Cells(1, activeCell.Column).Address, "$")(1)
  
    'Get the value of the active cell (i.e., the header value)
    headerValue = activeCell.Value
  
    'Construct the formula using the active cell's value and column letter
    myfmula = "= """ & headerValue & """. Tot: " & "COUNTA(" & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)" & " Vis: " & "AGGREGATE(3,3," & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)"
  
    'Insert the formula into the active cell
    activeCell.formula = myfmula
End Sub
 
Upvote 0
It probably means there is a problem with your formula.
You can insert a message box to return what the value of "myfmula" is to see if it looks like a valid formula.

So, if you add this line in your code, what exactly does the message box return?
Rich (BB code):
    'Construct the formula using the active cell's value and column letter
    myfmula = "= """ & headerValue & """. Tot: " & "COUNTA(" & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)" & " Vis: " & "AGGREGATE(3,3," & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)"
    MsgBox myfmula

Many times when you see what you built, the issue becomes evident.
 
Upvote 0
It probably means there is a problem with your formula.
You can insert a message box to return what the value of "myfmula" is to see if it looks like a valid formula.

So, if you add this line in your code, what exactly does the message box return?
Rich (BB code):
    'Construct the formula using the active cell's value and column letter
    myfmula = "= """ & headerValue & """. Tot: " & "COUNTA(" & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)" & " Vis: " & "AGGREGATE(3,3," & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)"
    MsgBox myfmula

Many times when you see what you built, the issue becomes evident.
It gives the following:
2022-12-19_09h30_17.png


Whereas expected is:

Excel Formula:
="XYZ. Tot: " & COUNTA(A4:A1000000) & " Vis: " & AGGREGATE(3,3,A4:A1000000)

Quotations are either in the wrong place or missing, and missing ampersands...
 
Upvote 0
Here is a little trick.

Turn on your Macro Recorder and record yourself entering your desired formula in a cell in your workbook.
Then stop the Macro Recorder and view the code you just recorded.
This will show you what the structure of your formula needs to look like in VBA (especially when it comes to double-quotes).
So compare that to the code you have now, and make the necessary adjustments.
 
Upvote 0
Here is a little trick.

Turn on your Macro Recorder and record yourself entering your desired formula in a cell in your workbook.
Then stop the Macro Recorder and view the code you just recorded.
This will show you what the structure of your formula needs to look like in VBA (especially when it comes to double-quotes).
So compare that to the code you have now, and make the necessary adjustments.

Right, I tried that earlier -- but it records in R1C1... That does not translate well when trying to apply that structure for the formula I am working with in this case.

Here's what the recording looks like:

VBA Code:
    ActiveCell.FormulaR1C1 = _
        "=""XYZ. Tot: "" & COUNTA(R[1]C:R[999997]C) & "" Vis: "" & AGGREGATE(3,3,R[1]C:R[999997]C)"
 
Upvote 0
Right, I tried that earlier -- but it records in R1C1... That does not translate well when trying to apply that structure for the formula I am working with in this case.

Here's what the recording looks like:

VBA Code:
    ActiveCell.FormulaR1C1 = _
        "=""XYZ. Tot: "" & COUNTA(R[1]C:R[999997]C) & "" Vis: "" & AGGREGATE(3,3,R[1]C:R[999997]C)"
But look closely at their use of double-quotes, especially at the beginning of the formula.
They have them doubled-up where you have them tripled up.
 
Upvote 0
But look closely at their use of double-quotes, especially at the beginning of the formula.
They have them doubled-up where you have them tripled up.
That's what I am saying, doesn't translate well in this case:

2022-12-19_11h08_16.png


with VBA line:

VBA Code:
myfmula = "="" & headerValue &  Tot: "" & COUNTA(" & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000) & "" Vis: "" & AGGREGATE(3,3," & columnLetter & activeCell.Row + 1 & ":" & columnLetter & "1000000)"
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,305
Members
449,218
Latest member
Excel Master

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