英文:
Bold the text till it finds $ or (
问题
我有一个单元格,其中包含实体代码和名称(如E01112 Japan),然后是金额(如$2或($2)),然后是一些文本(差异是由于),同样的模式再次出现在同一个单元格中。
E00011 China $56 是因为下雨 E00022 Italy ($45) 是因为阳光等等在一个单元格中。
我想要实体代码和它们的名称变成粗体。
我输入E0作为输入,并能够将实体代码变成粗体,但不能将实体名称变成粗体。
子 bold_text_start_string()
Dim r As Range
Dim cell As Range
Dim counter As Integer
Set r = Range("c1:c10")
text_value = InputBox("请输入要搜索并加粗的起始文本")
For Each cell In r
If InStr(cell.Text, text_value) Then
cell.Characters(WorksheetFunction.Find(text_value, cell.Value), Len(text_value) + 4).Font.Bold = True
End If
Next
英文:
I have a cell in which there are entity code and names(like E01112 Japan) then the amount(like $2 or ($2)) and then some text (variance is due) and again same pattern in same cell.
E00011 China $56 it is due to rain E00022 Italy ($45) It is due to sun and so on in one cell
I want the entity codes and their name to get bold.
I enter E0 as input and able to bold the entity code but not the entity name.
Sub bold_text_start_string()
Dim r As Range
Dim cell As Range
Dim counter As Integer
Set r = Range("c1:c10")
text_value = InputBox("Please Enter start Text You Want to Search and Bold")
For Each cell In r
If InStr(cell.Text, text_value) Then
cell.Characters(WorksheetFunction.Find(text_value, cell.Value), Len(text_value) + 4).Font.Bold = True
End If
Next
答案1
得分: 3
你可以尝试使用正则表达式来验证单元格中的模式,然后使用匹配对象的属性来开始编辑单元格。例如:
英文:
You could try to use a regular expression to validate your pattern in a cell and then use the match object's properties to start editing your cell. For example:
Sub Test()
Dim cl As Range: Set cl = Range("A1")
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\bE\d{5}\b.*?(?=\s*\(?$)"
Set REMatches = .Execute(cl.Value)
If REMatches.Count > 0 Then
For Each M In REMatches
cl.Characters(M.firstindex + 1, M.Length).Font.Bold = True
Next
End If
End With
End Sub
The pattern used:
\bE\d{5}\b.*?(?=\s*\(?$)
See an online demo
\bE\d{5}
- A capital 'E' with 5 trailing digits, between a leading and trailing word-boundary to assert the substring stands alone;.*?
- 0+ (lazy) Characters other than newline;(?=\s*\(?\$)
- Positive lookahead to assert match is followed by 0+ (greedy) whitespace characters, an optional literal open paranthesis and a literal '$'.
The above should result in:
> E00011 China $56 it is due to rain E00022 Italy ($45) It is due to sun and so on in one cell
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论