I'm trying to get ²
to show in a string. As an example my output should be inside a ActiveX
Textbox and should read R² = 50
.
I've tried the following two statements:
Selection.Characters.Text = "R&ChrW(&HB2)&" = " & variable
but this displays a 0
as the output in the Textbox. And this:
Selection.Characters.Text = "R² = " & variable
also displays a 0
.
This question is related to
vba
excel
excel-2007
No need to get too complicated. If all you need is ² then use the unicode representation.
http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
(which is how I assume you got the ² to appear in your question. )
I create equations with random numbers in VBA and for x squared put in x^2.
I read each square (or textbox) text into a string.
I then read each character in the string in turn and note the location of the ^ ("hats")'s in each.
Say the hats were at positions 4, 8 and 12.
I then "chop out" the first hat - the position of the character to be superscripted is now 4, the position of the other hats is now 7 and 11. I chop out the second hat, the character to superscript is now at 7 and the hat has moved to 10. I chop out the last hat .. the superscript character is now position 10.
I now select each character in turn and change the font to superscript.
Thus I can fill a whole spreadsheet with algebra using ^ and then call a routine to tidy it up.
For big powers like x to the 23 I build x^2^3 and the above routine does it.
Source: Stackoverflow.com