加粗文本直到找到 $ 或 (。

huangapple go评论88阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月11日 16:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定