vba 如何让平方符号(2)显示在字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13079534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the squared symbol (2) to display in a string
提问by aconnelly
I'm trying to get 2
to show in a string. As an example my output should be inside a ActiveX
Textbox and should read R2 = 50
.
我试图2
在字符串中显示。例如,我的输出应该在ActiveX
Textbox 内并且应该读取R2 = 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:
但这0
在文本框中显示为输出。和这个:
Selection.Characters.Text = "R2 = " & variable
also displays a 0
.
还显示一个0
.
采纳答案by Rory
Not sure what kind of text box you are refering to. However, I'm not sure if you can do this in a text box on a user form.
不确定您指的是哪种文本框。但是,我不确定您是否可以在用户表单的文本框中执行此操作。
A text box on a sheet you can though.
工作表上的文本框你可以。
Sheets("Sheet1").Shapes("TextBox 1").TextFrame2.TextRange.Text = "R2=" & variable
Sheets("Sheet1").Shapes("TextBox 1").TextFrame2.TextRange.Characters(2, 1).Font.Superscript = msoTrue
And same thing for an excel cell
对于 excel 单元格也是一样
Sheets("Sheet1").Range("A1").Characters(2, 1).Font.Superscript = True
If this isn't what you're after you will need to provide more information in your question.
如果这不是您所追求的,则需要在问题中提供更多信息。
EDIT: posted this after the comment sorry
编辑:在评论抱歉后发布此内容
回答by Brad
No need to get too complicated. If all you need is 2 then use the unicode representation.
没有必要变得太复杂。如果您只需要 2,则使用 unicode 表示。
http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
(which is how I assume you got the 2 to appear in your question. )
(这就是我假设你有 2 出现在你的问题中的方式。)
回答by Jim Beckwith
I create equations with random numbers in VBA and for x squared put in x^2.
我在 VBA 中创建了带有随机数的方程,并将 x 平方放入 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.
假设帽子在位置 4、8 和 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.
然后我“砍掉”第一顶帽子 - 要上标的角色的位置现在是 4,其他帽子的位置现在是 7 和 11。我砍掉第二顶帽子,要上标的角色现在是 7 和帽子已移至 10。我砍掉了最后一顶帽子 .. 上标字符现在是位置 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.
对于像 x 到 23 的大幂,我构建 x^2^3 并且上面的例程做到了。