excel - 将句子中的特定单词加粗,但不突出显示“,Lead Bookrunner”之前的整个句子
问题描述
句子 1:优步,$9.37MM 股权。Stifel Nicolaus, Lead Bookrunner。(我想在“.”之后和“, Lead Bookrunner”之前加粗字符,以及“,”之前的起始字符,即银行名称和公司名称,在这种情况下是 Stifel Nicolaus & Uber。 “.”也出现在 9.37MM Sentence 2: Google, $750MM Credit deal 中。摩根大通,首席账簿管理人。
我的代码:
Set Where = Range("P2", Range("P" & Rows.Count).End(xlUp))
With Where
.Value = .Value
.Font.Bold = False
End With
For Each This In Where
i = InStr(This, ", Lead Bookrunner.")
If i = 0 Then i = Len(This) + 1
This.Characters(1, i - 1).Font.Bold = True
Next
For Each This In Where
i = InStr(This, ".")
If i = 0 Then i = Len(This) + 1
This.Characters(1, i - 1).Font.Bold = False
Next
For Each This In Where
i = InStr(This, ",")
If i = 0 Then i = Len(This) + 1
This.Characters(1, i - 1).Font.Bold = True
Next
我通过我的代码得到的结果: 优步,9 美元。37MM 股权。Stifel Nicolaus,首席账簿管理人。得到我想要的结果:优步,$9.37MM 股权。Stifel Nicolaus,首席账簿管理人。
解决方案
格式是否总是像 TEXT COMMA TEXT FULLSTOP TEXT COMMA TEXT(Lead Bookrunner)?EX 1. TEXT (Uber) COMMA TEXT($9.37MM Equity) FULLSTOP TEXT (Stifel Nicolaus) COMMA TEXT(Lead Bookrunner) EX 2. TEXT (Google) COMMA TEXT($750MM Credit deal) FULLSTOP TEXT (JP Morgan) COMMA TEXT( Lead Bookrunner) – Siddharth Rout 25 分钟前
是的,这种格式将保持不变(TEXT COMMA TEXT FULLSTOP TEXT COMMA Lead Bookrunner。) – ah Pl 3 分钟前
如果格式要保持这样,那么它真的很简单。
逻辑:
代码:
我已经评论了代码。如果您仍然遇到困难,请随时在下面发表评论。
这是你正在尝试的吗?
Option Explicit
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim i As Long
Dim lPos As Long, rPosDot As Long, rPosComma As Long, strLen As Long
Dim cellValue As String
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find last row
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Loop through the range
For i = 1 To lRow
With .Range("A" & i)
'~~> Remove bold formatting
.Font.Bold = False
cellValue = .Value2
If InStr(1, cellValue, "(") Then
.Font.Bold = True
ElseIf InStr(1, cellValue, "Lead Bookrunner", vbTextCompare) Then
'~~> Store the length of the cell value
strLen = Len(cellValue)
'~~> Find the position of "," from left
lPos = InStr(1, cellValue, ",")
'~~> Find the position of "," from right
rPosComma = InStrRev(cellValue, ",", -1, vbTextCompare)
'~~> We need this so that we can find the position of "." on the
'~~> right of the ","
cellValue = Left(cellValue, rPosComma)
'~~> Find the position of "." from the right of ","
rPosDot = InStrRev(cellValue, ".", -1, vbTextCompare)
'~~> Bold the characters
.Characters(Start:=1, Length:=lPos - 1).Font.FontStyle = "Bold"
.Characters(Start:=rPosDot + 1, Length:=rPosComma - rPosDot - 1).Font.FontStyle = "Bold"
End If
End With
Next i
End With
End Sub
在行动:
推荐阅读
- python - 列表范围索引中的三个点是什么?
- java - Else 语句不起作用 [Ecplise IDE Java]
- mysql - mysql - 如何为列创建 id 列
- python - Pandas - 将列值转换为具有计数的新列
- terraform - aws_secretsmanager_secret 键/值对示例(基于 Terraform 文档)不适用于随机生成的密码
- javascript - 当您将鼠标悬停在图像上时,如何制作跟随您的显示文本效果
- reactjs - 在 REACT.js 中显示一个可回放的视频
- python - 异常类型错误类型错误:异常必须从 BaseException 派生
- python - 史派西“OSError:[E050] 找不到模型‘en_core_web_sm’。它似乎不是 Python 包或数据目录的有效路径。”
- javascript - 对象中的类构造函数可选参数