Solved

Formatting a text string created in Excel to include subscripts

Posted on 2011-03-03
7
303 Views
Last Modified: 2012-05-11
Some values generated with VBA code are used in a spreadsheet to produce a text string that is then used on a graph of the code's output as a data label marker

The text string is ="P"&TEXT(N15*100,"##")&"=$"&TEXT(N16,"#0.0")&"M" where the contents of the cells N15 and N16 are part of the code output

I want the two digits following the letter P to be subscripts to the P

Can I achieve this with formatting in Excel or can I create the same string in VBA with the subscript formatting so I could just write it to the label cell in the spreadsheet?

I know I can do it manually for static data but this has to be linked to the output of the code and update automatically
0
Comment
Question by:sjgrey
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 35032769
Try this

[a1] = "P" & Format([n15] * 100, "##") & "=$" & Format([n16], "#0.0") & "M"
[a1].Characters(Start:=1, Length:=2).Font.Subscript = True

Thomas
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 35032776
Actually, start:=2 not 1

[a1] = "P" & Format([n15] * 100, "##") & "=$" & Format([n16], "#0.0") & "M"
[a1].Characters(Start:=2, Length:=2).Font.Subscript = True

Thomas
0
 
LVL 1

Author Comment

by:sjgrey
ID: 35033128
Thanks for the swift response

Could you just clarify the notation for me please?  Does [a1] mean a VBA reference to the cell where the string is to wind up?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 39

Expert Comment

by:nutsch
ID: 35033186
Exactly, that notation could also be put

cells(row,column)
                          cells(1,1)

range("address")
                         range("A1")

So what I've given you is the code to put the result in cell A1. Do you need assistance in putting that in a macro / workbook?

T
0
 
LVL 1

Author Comment

by:sjgrey
ID: 35033294
I think I get it.  My code looks like this.

    
    Worksheets("Graph").Activate
    Cells(3, 2).Value = "P" & Format([n15] * 100, "##") & "=$" & Format([n16], "#0.0") & "M"
    Cells(3, 2).Characters(Start:=2, Length:=2).Font.Subscript = True

Open in new window


The only problem I now have is that, even with the following code to force the graph to update after calculating the data points it displays, the label that refers to the cell B3 or Cells(3,2) doesn't update.  It just shows the value it had when the reference was initially set up in the Select Data dialog.

    Application.Calculation = xlCalculationAutomatic
    Worksheets("Graph").Activate
    Application.Calculate
    

Open in new window


Changes in the numbers driving the graph are picked up.  The lines it draws are moved around when the inputs change, but the label on a data point used to label a marker on the graph is stuck. Labelling-bug.docx

The vertical line on the graph is in the right place as is a single point, which has no marker, that I place above the top of the line.  The label is attached to the single point but it isn't changing even though the contents of the cell it refers to have changed.  The subscripting worked perfectly thanks.

Any ideas on the label update?

I'm happy to start a fresh question if that would be more appropriate.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35037960
The label is hardcoded in the cell, which means it's no longer a formula and will only update when you run that macro. What you can do is put it in a worksheet_change macro so it will run every time a cell changes on that sheet.

Do you want help doing this or do you just want to rerun the macro each time?

Thomas
0
 
LVL 1

Author Comment

by:sjgrey
ID: 35040680
If there is an easy way to make the label pdate when the cell changes I'd be very interested and I don't know how to do that yet
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now