|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 批量上标test()
- '------------------------------------上面这部分主要是把原文字和上下标存入数组
- Dim doc As Document, rCount%, i%, j%, n%
- Dim Str_Original() As String, Str_Superscript() As String, Str_Subscript() As String
- Set doc = ActiveDocument: n = 1
- ReDim Preserve Str_Original(1 To n)
- ReDim Preserve Str_Superscript(1 To n) '上标
- ReDim Preserve Str_Subscript(1 To n) '下标
- Str_Original(1) = "KAl(SO4)2": Str_Superscript(1) = "AL": Str_Subscript(1) = "4,2" '下标不同时用逗号隔开
- 'Str_Original(2) = "KAl(SO4)2": Str_Superscript(2) = "AL": Str_Subscript(2) = "4,2"
- 'Str_Original(3) = "KAl(SO4)2": Str_Superscript(3) = "AL": Str_Subscript(3) = "4,2"
- '如果少的话,可以自己往后加
-
- '------------------------------------下面这部分主要遍历组数,用查找替换的方式分别对字符串上下标
- Dim Str_Start&, Str_End&, Rng As Range, Str_Superscript_Arr() As String, Str_Subscript_Arr() As String
- For i = 1 To UBound(Str_Original)
- With doc.Content.Find
- .Text = Str_Original(i)
- Do While .Execute
- Str_Start = .Parent.Start: Str_End = .Parent.End
- If Str_Superscript(i) <> "" Then
- Str_Superscript_Arr = Split(Str_Superscript(i), ",")
- For j = 0 To UBound(Str_Superscript_Arr)
- Set Rng = doc.Range(Str_Start, Str_End)
- With Rng.Find
- .Text = Str_Superscript_Arr(j)
- .Replacement.Font.Superscript = True
- .Execute Replace:=wdReplaceAll
- End With
- Next j
- End If
- If Str_Subscript(i) <> "" Then
- Str_Subscript_Arr = Split(Str_Subscript(i), ",")
- For j = 0 To UBound(Str_Subscript_Arr)
- Set Rng = doc.Range(Str_Start, Str_End)
- With Rng.Find
- .Text = Str_Subscript_Arr(j)
- .Replacement.Font.Subscript = True
- .Execute Replace:=wdReplaceAll
- End With
- Next j
- End If
- Loop
- End With
- Next i
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|