Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4995|回复: 8

一个单元格内某个符合条件的字符标色

[复制链接]
发表于 2019-5-22 13:40 | 显示全部楼层 |阅读模式
1学分
求教,比如一个单元格的内容是如下
08:11  11:40 12:06 17:08
我想给其中>08:00 或>12:00及小于17:00的某个字符标颜色
效果图是如下
08:11 11:40 12:06 17:08
有没什么代码的

最佳答案

查看完整内容

Sub test4() Dim i As Integer, t As Integer, n As Integer Dim rq As Date Dim rg As Range For Each rg In Range("a1", "a4:a6") '此处区域范围可以调整 i = Len(rg) - Len(Application.Substitute(rg, ":", "")) '求":"出现次数,即单元格内时间的个数 For t = 1 To i If t = 1 Then n = Application.Find(":", rg, 1) Else n = Application.Find(":", rg, n + 1) End If rq = TimeValue(Mid(rg, n - 2, 5)) ' ...
匿名  发表于 2019-5-22 13:40
Sub test4()
Dim i As Integer, t As Integer, n As Integer
Dim rq As Date
Dim rg As Range
For Each rg In Range("a1", "a4:a6")
'此处区域范围可以调整
i = Len(rg) - Len(Application.Substitute(rg, ":", ""))
'求":"出现次数,即单元格内时间的个数
For t = 1 To i
If t = 1 Then
n = Application.Find(":", rg, 1)
Else
n = Application.Find(":", rg, n + 1)
End If
rq = TimeValue(Mid(rg, n - 2, 5))
'求出每次时间的结果
If rq > TimeValue("08:00") And rq < TimeValue("12:00") Then
[rg].Characters(Start:=n - 2, Length:=5).Font.Color = vbRed
End If
'可调整时间范围
Next
Next
End Sub
回复

使用道具

发表于 2019-5-22 14:25 | 显示全部楼层
分隔的空格数注意一下,假设“08:11 11:40 12:06 17:08”在A1单元格内:
  1. Sub test()
  2. Dim arr, i&
  3. arr = Split([a1])
  4. For i = 0 To UBound(arr)
  5.   If arr(i) Like "08:*" Or arr(i) Like "12:*" Or arr(i) Like "16:*" Then
  6.     [a1].Characters(Start:=i * 6 + 1, Length:=5).Font.Color = vbRed
  7.   End If
  8. Next i
  9. End Sub
复制代码

有个小缺点。
回复

使用道具 举报

 楼主| 发表于 2019-5-22 14:53 | 显示全部楼层
大灰狼1976 发表于 2019-5-22 14:25
分隔的空格数注意一下,假设“08:11 11:40 12:06 17:08”在A1单元格内:

有个小缺点。

缺点就是只对a1单元格吗?不能适用于整个表吗
回复

使用道具 举报

 楼主| 发表于 2019-5-22 15:07 | 显示全部楼层
大灰狼1976 发表于 2019-5-22 14:25
分隔的空格数注意一下,假设“08:11 11:40 12:06 17:08”在A1单元格内:

有个小缺点。

大于08:00且小于11:30  ,或大于12:30且小于17:00 或大于19:00             代码应该怎么改
回复

使用道具 举报

发表于 2019-5-22 15:55 | 显示全部楼层
小缺点是用字符串方式判断,比如Like "08:*",它也能匹配"08:00",不符合大于的条件,是大于等于。
像你上面重新提出的要求,就不方便用字符串来判断了,必须用数值的形式,我再修改一下。
回复

使用道具 举报

发表于 2019-5-22 16:03 | 显示全部楼层
只对A1单元格这个不是什么问题,你自己应该也能修改。
  1. Sub test()
  2. Dim arr, i&, Tm As Date
  3. arr = Split([a1])
  4. For i = 0 To UBound(arr)
  5.   Tm = TimeValue(arr(i))
  6.   If (Tm > TimeValue("08:00") And Tm < TimeValue("11:30")) Or (Tm > TimeValue("12:30") And Tm < TimeValue("17:00")) Or Tm > TimeValue("19:00") Then
  7.     [a1].Characters(Start:=i * 6 + 1, Length:=5).Font.Color = vbRed
  8.   End If
  9. Next i
  10. End Sub
复制代码
回复

使用道具 举报

匿名  发表于 2019-5-23 17:31
Sub test4()
Dim i As Integer, t As Integer, n As Integer
Dim rq As Date
Dim rg As Range
On Error Resume Next
For Each rg In Range("a1,a4:a6")
'此处区域范围可以调整
i = Len(rg) - Len(Application.Substitute(rg, ":", ""))
'求":"出现次数,即单元格内时间的个数
For t = 1 To i
If t = 1 Then
n = Application.Find(":", rg, 1)
Else
n = Application.Find(":", rg, n + 1)
End If
rq = TimeValue(Mid(rg, n - 2, 5))
'求出每次时间的结果
If rq > TimeValue("08:00") And rq < TimeValue("12:00") Then
[rg].Characters(Start:=n - 2, Length:=5).Font.Color = vbRed
End If
'可调整时间范围
Next
Next
End Sub
回复

使用道具

 楼主| 发表于 2019-5-24 08:27 | 显示全部楼层
呓语 发表于 2019-5-22 13:40
Sub test4()
Dim i As Integer, t As Integer, n As Integer
Dim rq As Date

大神,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 12:55 , Processed in 0.406527 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表