?
Solved

Formatting a text string created in Excel to include subscripts

Posted on 2011-03-03
7
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

752 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