下标与上标冲突
Subscript vs. Superscript conflict
背景:
我正在尝试编写一个模块来将字符串与其格式连接起来。因此,我正在查看所有可能重要的 Font
属性,包括 Subscript
和 Superscript
.
示例数据:
想象一下 A1
:
示例代码:
Sub Test()
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
Next x
End With
End Sub
结果:
问题:
如果我使用 F8 逐步完成这段代码,我可以看到应该是下标的字符变成了下标,但是当传递上标值。反之亦然,这意味着上标属性保持不变。
这个过程是一个更大过程的一部分,例如我试图转换这个:
Sub ConcatStringsWithFormat()
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim props(9) As Variant, arr As Variant
Dim rng As Range
Dim x As Long, y As Long: y = 0
Set rng = Sheet1.Range("A1:A3")
With Application
.Trim (rng)
arr = rng: arr = .Transpose(.Index(arr, 0, 1))
End With
For Each cell In rng
If Len(cell) > 0 Then
y = y + 1
For x = 1 To cell.Characters.Count
props(0) = cell.Characters(x, 1).Font.Bold
props(1) = cell.Characters(x, 1).Font.ColorIndex
props(2) = cell.Characters(x, 1).Font.FontStyle
props(3) = cell.Characters(x, 1).Font.Italic
props(4) = cell.Characters(x, 1).Font.Size
props(5) = cell.Characters(x, 1).Font.Strikethrough
props(6) = cell.Characters(x, 1).Font.Subscript
props(7) = cell.Characters(x, 1).Font.Superscript
props(8) = cell.Characters(x, 1).Font.TintAndShade
props(9) = cell.Characters(x, 1).Font.Underline
dict.Add y, props
y = y + 1
Next x
End If
Next cell
With Sheet1.Cells(1, 2)
.Value = Application.Trim(Join(arr, " "))
For x = 1 To .Characters.Count
If Mid(.Value, x, 1) <> " " Then
.Characters(x, 1).Font.Bold = dict(x)(0)
.Characters(x, 1).Font.ColorIndex = dict(x)(1)
.Characters(x, 1).Font.FontStyle = dict(x)(2)
.Characters(x, 1).Font.Italic = dict(x)(3)
.Characters(x, 1).Font.Size = dict(x)(4)
.Characters(x, 1).Font.Strikethrough = dict(x)(5)
.Characters(x, 1).Font.Subscript = dict(x)(6)
.Characters(x, 1).Font.Superscript = dict(x)(7)
.Characters(x, 1).Font.TintAndShade = dict(x)(8)
.Characters(x, 1).Font.Underline = dict(x)(9)
End If
Next x
End With
End Sub
导致:
如您所见,丢失的只是下标属性。关于为什么会发生这种情况以及如何克服这种情况的任何想法?很明显,如果您手动尝试,一个单元格将允许两个属性在不同字符上都为真。
在设置这些属性之前进行测试:
Sub Test()
With Sheet1.Range("B2")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
If .Offset(0, -1).Characters(x, 1).Font.Subscript Then
.Characters(x, 1).Font.Subscript = True
ElseIf .Offset(0, -1).Characters(x, 1).Font.Superscript Then
.Characters(x, 1).Font.Superscript = True
End If
Next x
End With
End Sub
刚刚发现交换行会给出正确的结果:
错误
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
Next x
End With
对
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
Next x
End With
换行行得通。除了这些属性在单元格设置下也低于彼此之外没有其他解释。
背景:
我正在尝试编写一个模块来将字符串与其格式连接起来。因此,我正在查看所有可能重要的 Font
属性,包括 Subscript
和 Superscript
.
示例数据:
想象一下 A1
:
示例代码:
Sub Test()
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
Next x
End With
End Sub
结果:
问题:
如果我使用 F8 逐步完成这段代码,我可以看到应该是下标的字符变成了下标,但是当传递上标值。反之亦然,这意味着上标属性保持不变。
这个过程是一个更大过程的一部分,例如我试图转换这个:
Sub ConcatStringsWithFormat()
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim props(9) As Variant, arr As Variant
Dim rng As Range
Dim x As Long, y As Long: y = 0
Set rng = Sheet1.Range("A1:A3")
With Application
.Trim (rng)
arr = rng: arr = .Transpose(.Index(arr, 0, 1))
End With
For Each cell In rng
If Len(cell) > 0 Then
y = y + 1
For x = 1 To cell.Characters.Count
props(0) = cell.Characters(x, 1).Font.Bold
props(1) = cell.Characters(x, 1).Font.ColorIndex
props(2) = cell.Characters(x, 1).Font.FontStyle
props(3) = cell.Characters(x, 1).Font.Italic
props(4) = cell.Characters(x, 1).Font.Size
props(5) = cell.Characters(x, 1).Font.Strikethrough
props(6) = cell.Characters(x, 1).Font.Subscript
props(7) = cell.Characters(x, 1).Font.Superscript
props(8) = cell.Characters(x, 1).Font.TintAndShade
props(9) = cell.Characters(x, 1).Font.Underline
dict.Add y, props
y = y + 1
Next x
End If
Next cell
With Sheet1.Cells(1, 2)
.Value = Application.Trim(Join(arr, " "))
For x = 1 To .Characters.Count
If Mid(.Value, x, 1) <> " " Then
.Characters(x, 1).Font.Bold = dict(x)(0)
.Characters(x, 1).Font.ColorIndex = dict(x)(1)
.Characters(x, 1).Font.FontStyle = dict(x)(2)
.Characters(x, 1).Font.Italic = dict(x)(3)
.Characters(x, 1).Font.Size = dict(x)(4)
.Characters(x, 1).Font.Strikethrough = dict(x)(5)
.Characters(x, 1).Font.Subscript = dict(x)(6)
.Characters(x, 1).Font.Superscript = dict(x)(7)
.Characters(x, 1).Font.TintAndShade = dict(x)(8)
.Characters(x, 1).Font.Underline = dict(x)(9)
End If
Next x
End With
End Sub
导致:
如您所见,丢失的只是下标属性。关于为什么会发生这种情况以及如何克服这种情况的任何想法?很明显,如果您手动尝试,一个单元格将允许两个属性在不同字符上都为真。
在设置这些属性之前进行测试:
Sub Test()
With Sheet1.Range("B2")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
If .Offset(0, -1).Characters(x, 1).Font.Subscript Then
.Characters(x, 1).Font.Subscript = True
ElseIf .Offset(0, -1).Characters(x, 1).Font.Superscript Then
.Characters(x, 1).Font.Superscript = True
End If
Next x
End With
End Sub
刚刚发现交换行会给出正确的结果:
错误
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
Next x
End With
对
With Sheet1.Range("B1")
.Value = .Offset(0, -1).Value
For x = 1 To .Characters.Count
.Characters(x, 1).Font.Superscript = .Offset(0, -1).Characters(x, 1).Font.Superscript
.Characters(x, 1).Font.Subscript = .Offset(0, -1).Characters(x, 1).Font.Subscript
Next x
End With
换行行得通。除了这些属性在单元格设置下也低于彼此之外没有其他解释。