Convert formula to value in a table column using VBA

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
Been away from Excel for a while and now trying to get re-started ...

I have a worksheet table with numbers that I want to add a column to,
perform a calculation for each row in the table and then convert the cell contents to values.
I will add more columns to the sheet for other calculations.

1718814734941.png


I can add a new column with this Sub:
1718814951870.png

I know I can add a name parameter, but I plan to use this Sub repeatedly

It generates the following:
1718815087816.png


I want to create a Sub for each different calculation that does the following:
1.renames the header of the new column - for example, 5th to Rng
2.adds a calculation to the first row of the new column - for example, 4th number minus 1st number
(the table will then replicate that calculation for all the cells in the new column)
3.changes the formula to a value in the new cells

Hopefully ending up with something like:
1718815589385.png


Here are my 3 questions relating to VBA and tables:

A. I'm not sure how to rename the table column header from 5th to Rng

B. I'm not sure how move down 1 row to the first data row and
add ActiveCell.Formula = "=D2-A2", but using column header names 4th and 1st.

C. I'm also not sure how to convert the cell formulas to values.

I hope to create a Sub for each one of the calculations, something like:

1718816802367.png

I used to know how to do this but I've lost my notes and examples.
I think the 95 degree room I'm in has fried my brains ...

I realize that the add and convert can be their own subs
and be called. I'm open to suggestions
 

Attachments

  • 1718814873627.png
    1718814873627.png
    7 KB · Views: 1

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The macro recorder is your friend! I find table terminology totally baffling.

I reproduced your table and recorded steps. To rename the new column it recorded:
VBA Code:
    Range("Table1[[#Headers],[5th]]").Select
    ActiveCell.FormulaR1C1 = "Rng"
which can be changed to
VBA Code:
    Range("Table1[[#Headers],[5th]]").Value = "Rng"

Similarly, adding the formula produced
VBA Code:
    Range("E2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=[@1st]+[@4th]"
which can be changed to
VBA Code:
    Range("E2").FormulaR1C1 = "=[@1st]+[@4th]"

and so on.
 
Upvote 0
Your rename code worked.
Range("Table1[[#Headers],[5th]]").Value = "Rng"
but am having trouble with adding a formula.

Since I will be adding multiple columns later,
the range won't always be Range("E2").

I tried using
Range("Table1[[#Data],[Rng]]").Value = "=[4th]-[1st]"
but I get Run-time error '1004'
Application-defined or object-defined error.

Is there a simple way to avoid referencing cell E2 specifically
to move down 1 row from the Header column Rng ?
 
Upvote 0
Since I will be adding multiple columns later,
the range won't always be Range("E2").
Do you mean you want to refer to the last column of the table?
Maybe something like this:
VBA Code:
With ActiveSheet.ListObjects("Table1").DataBodyRange
    .Cells(1, .Columns.Count).Formula = "=" & .Cells(1, 4).Address(0, 0) & "-" & .Cells(1, 1).Address(0, 0)
End With
Example:
Book2
ABCDEF
1q1q2q3q4q5q6
2123453
312345
412345
Sheet3
Cell Formulas
RangeFormula
F2F2=D2-A2
 
Upvote 0
Do you mean you want to refer to the last column of the table?
Maybe something like this:
VBA Code:
With ActiveSheet.ListObjects("Table1").DataBodyRange
    .Cells(1, .Columns.Count).Formula = "=" & .Cells(1, 4).Address(0, 0) & "-" & .Cells(1, 1).Address(0, 0)
End With
Example:
Book2
ABCDEF
1q1q2q3q4q5q6
2123453
312345
412345
Sheet3
Cell Formulas
RangeFormula
F2F2=D2-A2
Perfect ! Exactly what I needed.
One last question ...
How would you convert the cells in the last column from a formula to a value?
 
Upvote 0
Do you mean you want to refer to the last column of the table?
Maybe something like this:
VBA Code:
With ActiveSheet.ListObjects("Table1").DataBodyRange
    .Cells(1, .Columns.Count).Formula = "=" & .Cells(1, 4).Address(0, 0) & "-" & .Cells(1, 1).Address(0, 0)
End With
Example:
Book2
ABCDEF
1q1q2q3q4q5q6
2123453
312345
412345
Sheet3
Cell Formulas
RangeFormula
F2F2=D2-A2
That's great. Is there a way to do that using structured references like "=q4-q1" ?
 
Upvote 0
That's great. Is there a way to do that using structured references like "=q4-q1" ?
Do you mean you want to refer to a column by its header?
My understanding is you're looking to use a formula on the last column where the formula makes reference to specific columns based on their headers and then convert the formula to value.
Here's an option:
VBA Code:
Sub gsinkinson_1()
Dim a As Range, b As Range, c As Range
With ActiveSheet.ListObjects("Table1")
    Set a = .ListColumns(.Range.Columns.Count).DataBodyRange 'get the last column DataBodyRange, i.e  City_6
    Set b = .ListColumns("City_4").DataBodyRange             'get City_4 DataBodyRange
    Set c = .ListColumns("City_1").DataBodyRange
End With
    
With a
    .Cells(1).Formula = "=" & b.Cells(1).Address(0, 0) & "-" & c.Cells(1).Address(0, 0)  'fill the formula in first cell in a
    .FillDown                                  'apply formula to the whole column
    .Value = .Value                            'convert formula to value
End With
End Sub
Example:
The result is in City_6
Book1
ABCDEF
1City_1City_2City_3City_4City_5City_6
2123453
3223452
4323451
Sheet1
 
Upvote 0
I wanted to thank you for your help.
After trying the suggestions, I decided to create a sub for each new column and calculation.
This is how I handled the first one:

AddSum.GIF


It turns out you don't have to replicate a formula into every cell of the column.
Placing the formula in any one cell of the table column will add it to every cell in the column.
I chose to put it in the 2nd row of the table.

That approach worked many additional columns, but seems to hang up with multi-parameter functions.
If I apply a formula manually as shown below, it works properly:
Manual.GIF


But when I try to do it in VBA, I get a syntax error:
WithVBA.GIF


Using structured references with Excel tables - Microsoft Support indicates escape characters are needed in formulas with [ ] # ' @.
It also suggests spaces for readability.


I tried using spaces : ActiveCell.Formula = "=COUNTIF(Table1[ @[Jan]:[Mar] ], "<50")" but it fails with the same error.

I tried : ActiveCell.Formula = "=COUNTIF(Table1'[ '@'[Jan']:'[Mar'] '], "<50")" but it fails with the same error.

Can you figure out where the error is?

If I can resolve this, I'll probably try to create a sub and pass it the name, formula, and format parameters.
Once I'm done adding all the columns, I convert the entire table's calculations to values.
 

Attachments

  • AddRng.GIF
    AddRng.GIF
    8.7 KB · Views: 0
Upvote 0

Forum statistics

Threads
1,217,981
Messages
6,139,762
Members
450,230
Latest member
RStasicky

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