Convert formula to value in a table column using VBA (2024)

  • If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.

  • Thread startergsinkinson
  • Start dateJun 19, 2024
  • Tags
    convert to valuetablesvba

G

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
  • Jun 19, 2024
  • #1

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.

Convert formula to value in a table column using VBA (1)

I can add a new column with this Sub:

Convert formula to value in a table column using VBA (2)

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

It generates the following:

Convert formula to value in a table column using VBA (3)

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:

Convert formula to value in a table column using VBA (4)

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:

Convert formula to value in a table column using VBA (5)

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

    7 KB· Views: 1

Excel Facts

Which Excel functions can ignore hidden rows?

The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Sort by dateSort by votes

myall_blues

Board Regular
Joined
Nov 26, 2015
Messages
241
Office Version
  1. 365
Platform
  1. Windows
  • Thursday at 12:49 AM
  • #2

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.

Upvote0

G

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
  • Thursday at 6:55 PM
  • #3

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 ?

Upvote0

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
5,226
Office Version
  1. 365
Platform
  1. Windows
  • Thursday at 11:09 PM
  • #4

gsinkinson said:

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

Upvote0

G

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
  • Saturday at 12:12 PM
  • #5

Akuini said:

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?

Upvote0

G

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
  • Saturday at 12:20 PM
  • #6

Akuini said:

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" ?

Upvote0

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
5,226
Office Version
  1. 365
Platform
  1. Windows
  • Saturday at 6:46 PM
  • #7

gsinkinson said:

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 RangeWith 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").DataBodyRangeEnd 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 valueEnd WithEnd Sub

Example:
The result is in City_6

Book1
ABCDEF
1City_1City_2City_3City_4City_5City_6
2123453
3223452
4323451

Sheet1

Upvote0

G

gsinkinson

New Member
Joined
Nov 8, 2011
Messages
5
  • Today at 11:28 AM
  • #8

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:

Convert formula to value in a table column using VBA (10)

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:

Convert formula to value in a table column using VBA (11)

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

Convert formula to value in a table column using VBA (12)

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

    8.7 KB· Views: 0

Upvote0

You must log in or register to reply here.

Similar threads

T

  • Question

Cant convert to True numbers

  • thedeadzeds
  • May 17, 2024
  • Excel Questions
Replies
1
Views
205

May 17, 2024

Alex Blakenburg

A

D

  • Solved

VBA Delete Table Rows Given Criteria

  • Damien Hartzell
  • Jun 9, 2024
  • Excel Questions

2

Replies
10
Views
178

Jun 10, 2024

igold

  • Question

VBA adding values to new table rows

  • duteberta
  • Apr 10, 2024
  • Excel Questions
Replies
8
Views
451

Apr 11, 2024

Domenic

D

B

  • Question

Add new Row in ListObject Table with data from an Array

  • Bobbo4518
  • Apr 14, 2024
  • Excel Questions
Replies
4
Views
413

Apr 14, 2024

Akuini

  • Question

Row dropdown triggers converting entire table row to values

  • duteberta
  • Mar 7, 2024
  • Excel Questions
Replies
4
Views
191

Mar 7, 2024

Eric W

Forum statistics

Threads
1,217,850
Messages
6,138,982
Members
450,171
Latest member
anborroms

Share this page

  • Forums
  • Question Forums
  • Excel Questions
Convert formula to value in a table column using VBA (2024)

References

Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5959

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.