Solved

Formatting a text string created in Excel to include subscripts

Posted on 2011-03-03
7
333 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

729 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