在一个string中混合下标
在我使用的VBA excelmacros中,我有以下代码:
For k = MinDeg To MaxDeg OutputStr = Trim(OutputStr & "a" & Str(k) & " = " & _ Str(MyCoe) & " ± " & _ Str(MyErr) & Chr(10)) Next k
“MyCoe”和“MyErr”是数字,“minDeg”和“MaxDeg”是整数。
我的问题是:
如何使“Str(k)”作为下标文本出现在outputstr中?
如果Unicode在您的环境中可用,则另一种select是使用Str(K)的下标Unicode字符。 对Gary的学生代码进行一些修改,以便在A1中获得输出:
Option Explicit Sub foo() Dim K As Long Const MinDeg As Long = 10 Const MaxDeg As Long = 13 Dim sK As String, I As Long Const MyCoe As Long = 3 Const MyErr As Long = 5 Dim OutPutStr As String For K = MinDeg To MaxDeg sK = "" For I = 1 To Len(CStr(K)) sK = sK & ChrW(832 & Mid(CStr(K), I, 1)) Next I OutPutStr = Trim(OutPutStr & "a" & sK & " = " & _ Str(MyCoe) & " ± " & _ Str(MyErr) & Chr(10)) Next K Cells(1, 1) = OutPutStr End Sub
请注意下标值也出现在公式栏中。
首先我运行这个简单的mod到你的代码中:
Sub WhatEverr() mindeg = 10 maxdeg = 13 mycoe = 3 myerr = 5 For k = mindeg To maxdeg outputstr = Trim(outputstr & "a" & Str(k) & " = " & _ Str(mycoe) & " ± " & _ Str(myerr) & Chr(10)) Next k Range("A1").Value = outputstr End Sub
在A1中得到这个:
然后我运行:
Sub formatcell() Dim i As Long, L As Long, rng As Range Dim s As String Set rng = Range("A1") s = rng.Value L = Len(s) For i = 1 To L ch = Mid(s, i, 1) If ch = "a" Then rng.Characters(Start:=i + 2, Length:=2).Font.Subscript = True End If Next i End Sub
要应用格式:
在Excel中,这种types的字符格式是Range
对象的一个属性。 你不会像在HTML中那样将其构build到string中。