Construct a string containing formulas in VBA?

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
This is a fun one.

I'm updating a suite of numbers on a sheet with the new month's data, using VBA:


VBA Code:
LUC = ActiveCell.Column
endmo = LUC - 1
EndmoNam = Cells(2, LUC)


met.Activate

x = 3

Range("B9").Activate

Do Until ActiveCell.Row > 32

    Do Until ActiveCell.Column > 6
    
    If ActiveCell.Column < 7 Then
        ActiveCell.FormulaR1C1 = "=+Workings!R" & x & "C" & LUC & ""
        'ActiveCell.Offset(1, 0).Value = Format(EndmoNam, "mmm-yy") & " " & ""
        x = x + 1
    End If
    
    ActiveCell.Offset(0, 2).Activate
    Loop
ActiveCell.Offset(5, -6).Activate
Loop

Above the commented line is where I update the main figure for each metric. So on the Workings tab, on row 3 (x = 3 to start) and in column "LUC" (Look up column) it finds the main figure.

Below that, where the commented line is, is where I want to update the secondary figure which is a string.

The string currently looks like this:

Jul-21 £98k (+32k)

To break it down, "Jul-21" is held as a number formatted as date (EndmoNam), the update for "£98k" held to the column left of LUC (endmo) and the figure in brackets is held to the right of the LUC.


So what I'm trying to do in effect is:

VBA Code:
activecell.offset(1,0).value = Format(EndmoNam, "mmm-yy") & Workings!R" & x & "C" & Endmo & "(" & Workings!R" & x & "C" & LUC + 1 & ")"


But it's obviously not working because a value won't take a formula into consideration.

Either I need to use a formula, and somehow convert the value, or use .value and convert the formula.

Is there a way to take the EndMoNam (which may be the Clng of 01/07/2021) and convert it to a string?


Thanks.
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Missed a couple of ampersands:

Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & """ & Workings!R" & x & "C" & LUC & "&""("" & Workings!R" & x & "C" & LUC + 1 & "&"")"""

Okie, I now have this:
VBA Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & " "" & Workings!R" & x & "C" & LUC - 1 & "&""("" & Workings!R" & x & "C" & LUC + 1 & "&"")"""

Which results in
Excel Formula:
Jul-21 179.69(67.20137)

What I need to do is have this formatting.

Excel Formula:
Jul-21 £179.7k (+£67.2k)

But when I try to wrap a format around one of the Workings! formulas, it fails, I think it needs to go between the double quotation marks before the "& Workings!" as that's the only place where the autocomplete starts to pop up. Can't work out where the end of it goes. Thanks.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Here's the formatting I need

+£#,##0.0"K";-£#,##0.0"K";£#,##0.0"K"
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Here's the formatting I need

+£#,##0.0"K";-£#,##0.0"K";£#,##0.0"K"
And here it is...VBA..itised...

VBA Code:
Format(ActiveCell.Value, "+£#,##0.00""" & "K" & """;-£#,##0.00""" & "K" & """;£#,##0.00""" & "K" & """")
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,821
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Since it's part of a formula, you need to add the TEXT function to format it:

Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & " "" & TEXT(Workings!R" & x & "C" & LUC - 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&""("" & TEXT(Workings!R" & x & "C" & LUC + 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&"")"""
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Since it's part of a formula, you need to add the TEXT function to format it:

Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & " "" & TEXT(Workings!R" & x & "C" & LUC - 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&""("" & TEXT(Workings!R" & x & "C" & LUC + 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&"")"""
Hi Rory,

When I put that in, it has a problem, so I've amended to:

VBA Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & " "" & TEXT(Workings!R" & x & "C" & LUC - 1 & ",""+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&""("" & TEXT(Workings!R" & x & "C" & LUC + 1 & ",""+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&"")"""

Feel like we're close, but this again says application or object error.
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Since it's part of a formula, you need to add the TEXT function to format it:

Code:
ActiveCell.Offset(1, 0).FormulaR1C1 = "=""" & Format(endmo, "mmm-yy") & " "" & TEXT(Workings!R" & x & "C" & LUC - 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&""("" & TEXT(Workings!R" & x & "C" & LUC + 1 & ","+£#,##0.0""K"";-£#,##0.0""K"";£#,##0.0""K"")&"")"""

I'm so dumb, I didn't realise that TEXT wasn't a function in Excel and that what you've written was it actually laying the formula in manually via VBA.

Cool, we're on the same page. I thought, I'll use what I've currently got and then manually edit the format in the formula bar to add the text, and then finally use a macro recorded so I don't need to blow my brains out figuring which quote mark I've missed and where.

So I now have this:

Excel Formula:
="Jul-21 " & TEXT(Workings!$AP$3,+£#,##0.0"K";-£#,##0.0"K";£#,##0.0"K")&"(" & TEXT(Workings!$AR$3,+£#,##0.0"K";-£#,##0.0"K";£#,##0.0"K")&")"

But it's saying "there's a problem with the formula" and this part is now highlighted:

£#
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Here's the formatting I need

+£#,##0.0"K";-£#,##0.0"K";£#,##0.0"K"

It appears that THIS formatting, does not work in TEXT function, when you add a "semicolon" it stops it working.
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
="Jul-21 " & TEXT(Workings!$AP$3,"£#,##0.0")&"K (" &IF(Workings!$AQ$3<Workings!$AP$3,"-","+")& TEXT(Workings!$AR$3,"£#,##0.0")&"K)"

Solved. Semicolons don't work with TEXT(), so I had to go medieval.

I also excluded the "K" from the formatting too, this is much cleaner.

Thanks again for the help.
 

Forum statistics

Threads
1,148,280
Messages
5,745,829
Members
423,981
Latest member
ph1l

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
Top