如何仅在Excel中有条件地格式化子字符串 [英] How to conditionally format substring only in Excel

查看:84
本文介绍了如何仅在Excel中有条件地格式化子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在A1中说包含 男孩 ,在B1中说 <这是一个男孩 ,我想根据 A1 B1 中搜索子字符串,并在<$ c $中仅格式化子字符串(仅 boy) c> B1 ,而不是 B1 中的整个句子。
如果使用 = Search(A1,B1) = Isnumber(Search(A1,B1))在条件格式中,它将格式化整个句子,这不是我想要的。请告诉我如何解决这个问题。谢谢。

解决方案

据我所知,这只能使用VBA来实现。尝试以下(原始)子操作:

  Sub bold()
出错时继续操作'否则,您将收到一个如果子字符串丢失
对于选定的每个单元格
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'当您更改其他内容时希望
继续:
下一个单元格
结束子

与您在上面定义的电子表格的设置相同(要格式化的子字符串字符串的左侧),并且需要在运行宏之前选择子字符串。 / p>

Say in A1 contains "boy", in B1 contains "This is a boy", I want to search substring in B1 based on A1, and format the substring only ("boy" only) in B1, not the whole sentence in B1. If using =Search(A1,B1) or =Isnumber(Search(A1,B1)) in Conditonal Formatting, it will format the whole sentence, that is not what I want. Please kindly tell me how to solve this problem. Thank you.

解决方案

As far as I know, this is only possible using VBA. Try the following (crude) sub:

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

This expects the same setup of the spreadsheet you defined above (with the substring to be formatted to the left of the string) and requires that you select the substrings before running the macro.

这篇关于如何仅在Excel中有条件地格式化子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆