VBA 用于在定义的字符串中将 2 更改为上标
VBA For Changing 2 to a Superscript when it's in a defined string
所以我尝试创建一个代码来对长数据集的 headers 进行一些基本格式化。我有一个 fixsymbol 代码,可以使用正确的符号将 um 更改为千分尺,但是当我尝试 运行 下面的代码将 (um2) 更改为上标 2 时,它会闪烁 Error 13 type mismatch 并且调试会突出显示这一行"Position = InStr(c.Value, "µm2")" 它仍然是 运行 代码,但在最后吐出错误,如果我尝试 运行 它在原始数据集上不在 table 中它崩溃了 Excel。我将如何修复此错误,以便我可以将其 运行 作为更大脚本的一部分而不会崩溃?
Sub ChangeToSuperScript()
Dim X As Long
Dim Position As Long
Dim c As Range
For Each c In Range("A:Z") 'range of cells
Position = InStr(c.Value, "µm2") 'change the number 2 into a potentiation sign
If Position Then c.Characters(Position + 2, 1).Font.Superscript = True
Next
End Sub
谢谢!
您可以筛选出错误值。
编辑:更新为使用 SpecialCells
仅对固定值进行操作...
Sub ChangeToSuperScript()
Dim ws As Worksheet, rng As Range
Dim Position As Long, c As Range
Set ws = ActiveSheet
On Error Resume Next 'ignore error if no constants
Set rng = ws.Cells.SpecialCells(xlCellTypeConstants)
On Error GoTo 0 'stop ignoring errors
If rng Is Nothing Then Exit Sub 'no fixed values
On Error GoTo haveError
Application.Calculation = xlCalculationManual
For Each c In rng.Cells
Position = InStr(c.Value, "µm2")
Debug.Print c.Address, c.Value, Position
If Position > 0 Then
c.Characters(Position + 2, 1).Font.Superscript = True
End If
Next
Application.Calculation = xlCalculationAutomatic
Exit Sub
haveError:
Debug.Print "Error:" & Err.Description
Application.Calculation = xlCalculationAutomatic
End Sub
所以我尝试创建一个代码来对长数据集的 headers 进行一些基本格式化。我有一个 fixsymbol 代码,可以使用正确的符号将 um 更改为千分尺,但是当我尝试 运行 下面的代码将 (um2) 更改为上标 2 时,它会闪烁 Error 13 type mismatch 并且调试会突出显示这一行"Position = InStr(c.Value, "µm2")" 它仍然是 运行 代码,但在最后吐出错误,如果我尝试 运行 它在原始数据集上不在 table 中它崩溃了 Excel。我将如何修复此错误,以便我可以将其 运行 作为更大脚本的一部分而不会崩溃?
Sub ChangeToSuperScript()
Dim X As Long
Dim Position As Long
Dim c As Range
For Each c In Range("A:Z") 'range of cells
Position = InStr(c.Value, "µm2") 'change the number 2 into a potentiation sign
If Position Then c.Characters(Position + 2, 1).Font.Superscript = True
Next
End Sub
谢谢!
您可以筛选出错误值。
编辑:更新为使用 SpecialCells
仅对固定值进行操作...
Sub ChangeToSuperScript()
Dim ws As Worksheet, rng As Range
Dim Position As Long, c As Range
Set ws = ActiveSheet
On Error Resume Next 'ignore error if no constants
Set rng = ws.Cells.SpecialCells(xlCellTypeConstants)
On Error GoTo 0 'stop ignoring errors
If rng Is Nothing Then Exit Sub 'no fixed values
On Error GoTo haveError
Application.Calculation = xlCalculationManual
For Each c In rng.Cells
Position = InStr(c.Value, "µm2")
Debug.Print c.Address, c.Value, Position
If Position > 0 Then
c.Characters(Position + 2, 1).Font.Superscript = True
End If
Next
Application.Calculation = xlCalculationAutomatic
Exit Sub
haveError:
Debug.Print "Error:" & Err.Description
Application.Calculation = xlCalculationAutomatic
End Sub