Better way to input formulas in tables?

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
So I have 8 different tables, some with up to 65 columns, others less.

Current I have formulas in the columns for those which need formulas.

I kind of want to transition away from having the formulas in-cell, where they can be changed, erased, etc., to having them be coded.

However, I am trying to figure out what is the "best-practice" for coding formulas into cells/columns from VBA, specifically for tables (if that makes a difference).

I am currently doing it this way:

Code:
ListObject.ListColumns("Column name").DataBodyRange.FormulaR1C1 = "new formula"

It works, but it seems kind of ..... clunky (???) to me.

Is there a proper way, or a standardized way, or perhaps a more efficient way to do it?

Granted, I have like 70+ different formulas across all tables, some similar others unique.

Am I going to run into any issues by doing it the way I wrote above?

Thanks for the input!

-Spydey

P.S. The tables will be growing and shrinking as data is added/removed from them. I don't think this will have an effect at all but I thought it was important to know.
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
So I already ran into one issue.

I tried inputting one of my more lengthy formulas and it comes back with an error message. I believe due to the "" that I am using in-formula.

So I tried a more simple formula.

Code:
Sub Summary()

Dim tblSummary As ListObject
Dim wb As Workbook
Dim ws As Worksheet


Set wb = ThisWorkbook
Set ws = wb.Worksheets("Summary")


ws.ListObjects("Summary").ListColumns("Score").DataBodyRange.FormulaR1C1 = "=5+6"




End Sub

That works.

But when I do the below, it fails.

Code:
Sub Summary()

Dim tblSummary As ListObject
Dim wb As Workbook
Dim ws As Worksheet


Set wb = ThisWorkbook
Set ws = wb.Worksheets("Summary")


ws.ListObjects("Summary").ListColumns("Score").DataBodyRange.FormulaR1C1 = "=If(Summary[State] = "TX","Yes","No"




End Sub

It fails stating:

"Compile Error. Expected end of statement."

It highlights the YES as the point of failure.

Any thoughts?

-Spydey
 
Last edited:
Upvote 0
Ok, I think that I figured it out. I need double quotes where ever there are single quotes in my formula. That was pretty simple ..... I just hope there aren't other issues.

-Spydey
 
Upvote 0
So ran into a new issue.

The majority of my in-cell formulas are entered as array (CSE) formulas.

Even changing the .FormulaR1C1 to .FormulaArray, and changing all the single quotes (") to double quotes ("") in the formula, gives me an error.

I think it might be due to the tables ... but I am not sure. Gotta test a few things .... Any pointers would be much appreciated.

-Spydey
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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