excel 中 如何把单元格中的部分内容设置 格式, 通过 条件格式 或者 VBA
比如A1,A2两列数据都是文本内容A1为中国A2为我爱中国,能不能当A2中有A1的内容时这部分在A2中加粗!结果为我爱中国,中国两个字加粗呢...
比如 A1,A2 两列数据 都是文本内容 A1为中国 A2 为 我爱中国 ,能不能 当A2中有A1的内容时 这部分在 A2 中加粗! 结果为 我爱中国,中国两个字加粗呢
展开
5个回答
展开全部
VBA不会,刚试验出一种用“条件格式”的方法,操作起来更加傻瓜式:
A1,A2怎么会是两列数据呢?我下面的步骤,暂且按照A和B两列数据写了哈,要达到目的是,如果B1中有A1的内容,就把B1加粗。B2等等以此类推:
全选B列--“开始”选项卡--“条件格式”--“新建规则”--“使用公式确定要设置格式的单元格”,
在“为符合此公式的值设定格式”那个文本框里,填写:
=not(iserror(findb(A1,B1)))
按下“格式”按钮,选择“加粗”
确定,就ok了。
你试试吧,如果移植不到你的excel里,hi我帮你做
A1,A2怎么会是两列数据呢?我下面的步骤,暂且按照A和B两列数据写了哈,要达到目的是,如果B1中有A1的内容,就把B1加粗。B2等等以此类推:
全选B列--“开始”选项卡--“条件格式”--“新建规则”--“使用公式确定要设置格式的单元格”,
在“为符合此公式的值设定格式”那个文本框里,填写:
=not(iserror(findb(A1,B1)))
按下“格式”按钮,选择“加粗”
确定,就ok了。
你试试吧,如果移植不到你的excel里,hi我帮你做
展开全部
用excel的宏就能实现
以下是代码,如果是部分单元格都要改,写一个循环就行了
Sub Macro3()
Cells.Find(What:=Range("A1"), After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "我爱你中国"
With ActiveCell.Characters(Start:=1, Length:=3).Font
.Name = "宋体"
.FontStyle = "常规"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With ActiveCell.Characters(Start:=4, Length:=2).Font
.Name = "宋体"
.FontStyle = "加粗"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("A3").Select
End Sub
以下是代码,如果是部分单元格都要改,写一个循环就行了
Sub Macro3()
Cells.Find(What:=Range("A1"), After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "我爱你中国"
With ActiveCell.Characters(Start:=1, Length:=3).Font
.Name = "宋体"
.FontStyle = "常规"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With ActiveCell.Characters(Start:=4, Length:=2).Font
.Name = "宋体"
.FontStyle = "加粗"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("A3").Select
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
假如你是想在sheet1中实现此功能:
Alt+F11打开代码输入窗口,双击左边sheet1,在右边代码框输入下面代码,关闭回到excel即可.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i%, L1%, L2%, st1$, st2$
If Target.Column <> 1 Then Exit Sub
If Target.Row <> 2 Then Exit Sub
st1 = Target.Offset(-1, 0)
st2 = Target
L1 = Len(st1)
L2 = Len(st2)
If L1 = 0 Or L2 < L1 Then Exit Sub
Application.EnableEvents = False
For i = 1 To L2 - L1 + 1
If Mid(st2, i, L1) = st1 Then
Target.Characters(Start:=i, Length:=L1).Font.FontStyle = "粗体"
i = i + L1 - 1
End If
Next i
Application.EnableEvents = True
End Sub
要实现A和B列(第二行开始)对应所述改变的代码如下:(供参考)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i%, L1%, L2%, st1$, st2$
If Target.Column <> 2 Then Exit Sub
If Target.Row = 1 Then Exit Sub
st1 = Target.Offset(0, -1)
st2 = Target
L1 = Len(st1)
L2 = Len(st2)
If L1 = 0 Or L2 < L1 Then Exit Sub
Application.EnableEvents = False
For i = 1 To L2 - L1 + 1
If Mid(st2, i, L1) = st1 Then
Target.Characters(Start:=i, Length:=L1).Font.FontStyle = "粗体"
i = i + L1 - 1
End If
Next i
Application.EnableEvents = True
End Sub
Alt+F11打开代码输入窗口,双击左边sheet1,在右边代码框输入下面代码,关闭回到excel即可.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i%, L1%, L2%, st1$, st2$
If Target.Column <> 1 Then Exit Sub
If Target.Row <> 2 Then Exit Sub
st1 = Target.Offset(-1, 0)
st2 = Target
L1 = Len(st1)
L2 = Len(st2)
If L1 = 0 Or L2 < L1 Then Exit Sub
Application.EnableEvents = False
For i = 1 To L2 - L1 + 1
If Mid(st2, i, L1) = st1 Then
Target.Characters(Start:=i, Length:=L1).Font.FontStyle = "粗体"
i = i + L1 - 1
End If
Next i
Application.EnableEvents = True
End Sub
要实现A和B列(第二行开始)对应所述改变的代码如下:(供参考)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i%, L1%, L2%, st1$, st2$
If Target.Column <> 2 Then Exit Sub
If Target.Row = 1 Then Exit Sub
st1 = Target.Offset(0, -1)
st2 = Target
L1 = Len(st1)
L2 = Len(st2)
If L1 = 0 Or L2 < L1 Then Exit Sub
Application.EnableEvents = False
For i = 1 To L2 - L1 + 1
If Mid(st2, i, L1) = st1 Then
Target.Characters(Start:=i, Length:=L1).Font.FontStyle = "粗体"
i = i + L1 - 1
End If
Next i
Application.EnableEvents = True
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码简单一点也能实现.
右键单击工作表标签,在右键菜单中选择"查看代码",在代码编辑区复制粘贴以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Or Target.Row = 1 Then Exit Sub
Dim n%, c%
If Target.Count = 1 Then
c = Len(Target(1, 0))
If c > 0 Then n = InStr(Target, Target(1, 0))
If n > 0 Then
Target.Characters(Start:=n, Length:=c) _
.Font.FontStyle = "加粗"
End If
End If
End Sub
注意:将输入列(B列)设置为文本格式,否则输入数值时A列对应单元格也是数值,就不能有效了.
右键单击工作表标签,在右键菜单中选择"查看代码",在代码编辑区复制粘贴以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Or Target.Row = 1 Then Exit Sub
Dim n%, c%
If Target.Count = 1 Then
c = Len(Target(1, 0))
If c > 0 Then n = InStr(Target, Target(1, 0))
If n > 0 Then
Target.Characters(Start:=n, Length:=c) _
.Font.FontStyle = "加粗"
End If
End If
End Sub
注意:将输入列(B列)设置为文本格式,否则输入数值时A列对应单元格也是数值,就不能有效了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Sub 校对()
Dim RNG As Range
Dim ROW1 As Integer
Dim ROW2 As Integer
Dim COL1 As Integer
Set RNG = Selection
If RNG Is Nothing Then
MsgBox ("请选择需要校对的区域!")
End If
ROW1 = RNG.Row
ROW2 = RNG.Rows.Count + RNG.Row - 1
COL1 = RNG.Column
For I = ROW1 To ROW2
For J = 1 To Len(Cells(I, COL1 + 1))
If InStr(Cells(I, COL1).Value, Mid(Cells(I, COL1 + 1), J, 1)) > 0 Then
Cells(I, COL1 + 1).Characters(Start:=J, Length:=1).Font.FontStyle = "加粗"
End If
Next J
Next I
End Sub
以上是VBA代码,先选择需要校对的区域,然后运行这个。经测试,OK!如有疑问可QME13745897
Dim RNG As Range
Dim ROW1 As Integer
Dim ROW2 As Integer
Dim COL1 As Integer
Set RNG = Selection
If RNG Is Nothing Then
MsgBox ("请选择需要校对的区域!")
End If
ROW1 = RNG.Row
ROW2 = RNG.Rows.Count + RNG.Row - 1
COL1 = RNG.Column
For I = ROW1 To ROW2
For J = 1 To Len(Cells(I, COL1 + 1))
If InStr(Cells(I, COL1).Value, Mid(Cells(I, COL1 + 1), J, 1)) > 0 Then
Cells(I, COL1 + 1).Characters(Start:=J, Length:=1).Font.FontStyle = "加粗"
End If
Next J
Next I
End Sub
以上是VBA代码,先选择需要校对的区域,然后运行这个。经测试,OK!如有疑问可QME13745897
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询