如何仅在 Excel 中有条件地格式化子字符串
How to conditionally format substring only in Excel
说A1中包含"boy"
,B1中包含"This is a boy"
,我想根据A1
搜索B1
中的子字符串,并仅格式化子字符串("boy" only) in B1
, 而不是 B1
中的整个句子。
如果在条件格式中使用=Search(A1,B1)
或=Isnumber(Search(A1,B1))
,它将格式化整个句子,这不是我想要的。请告诉我如何解决这个问题。谢谢。
据我所知,这只能使用 VBA。尝试以下(原始)子:
Sub bold()
On Error GoTo continue 'Otherwise you will receive an error if the substring is missing
For Each cell In Selection
myString = cell.Offset(0, 1)
mySearch = cell
mySearch_length = Len(mySearch)
mySearch_startPos = WorksheetFunction.Search(mySearch, myString)
cell.Offset(0, 1).Characters(Start:=mySearch_startPos, Length:=mySearch_length).Font.FontStyle = "Bold" 'Change to something else as you wish
continue:
Next cell
End Sub
这需要与您在上面定义的电子表格相同的设置(要格式化的子字符串位于字符串的左侧)并要求您 select 宏 运行 之前的子字符串。
说A1中包含"boy"
,B1中包含"This is a boy"
,我想根据A1
搜索B1
中的子字符串,并仅格式化子字符串("boy" only) in B1
, 而不是 B1
中的整个句子。
如果在条件格式中使用=Search(A1,B1)
或=Isnumber(Search(A1,B1))
,它将格式化整个句子,这不是我想要的。请告诉我如何解决这个问题。谢谢。
据我所知,这只能使用 VBA。尝试以下(原始)子:
Sub bold()
On Error GoTo continue 'Otherwise you will receive an error if the substring is missing
For Each cell In Selection
myString = cell.Offset(0, 1)
mySearch = cell
mySearch_length = Len(mySearch)
mySearch_startPos = WorksheetFunction.Search(mySearch, myString)
cell.Offset(0, 1).Characters(Start:=mySearch_startPos, Length:=mySearch_length).Font.FontStyle = "Bold" 'Change to something else as you wish
continue:
Next cell
End Sub
这需要与您在上面定义的电子表格相同的设置(要格式化的子字符串位于字符串的左侧)并要求您 select 宏 运行 之前的子字符串。