Solved

Formatting a text string created in Excel to include subscripts

Posted on 2011-03-03
7
318 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

856 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