如何突出显示excel中的选定文本 [英] How to highlight selected text within excel

查看:29
本文介绍了如何突出显示excel中的选定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 VBA 函数来突出显示 Excel 单元格中的特定文本.这可能吗?我一直在谷歌搜索,但目前还不清楚.

I would like to write a VBA function to highlight specific text within an excel cell. Is this possible? I've been googling but it's unclear at this point.

澄清一下,我想在特定列中搜索文本值(实际上是值列表)并以黄色突出显示匹配的文本.

to clarify, I would like to search a specific column for a text value (actually a list of values) and highlight the matched text in say yellow.

注意:这是我最终做的:

Note: this is what I ended up doing:

  Sub Colors()


    Dim searchString As String
    Dim targetString As String
    Dim startPos As Integer

    searchString = "abc"
    targetString = Cells(2, 1).Value
    startPos = InStr(targetString, searchString)

    If startPos > 0 Then

        Cells(2, 1).Characters(startPos, Len(searchString)).Font.Color = vbRed

    End If


 End Sub

推荐答案

这是基本原则,我假设自定义此代码不是您所要求的(因为没有提供有关此的详细信息):

This is the basic principle, I assume that customizing this code is not what you are asking (as no details about this were provided):

 Sub Colors()

 With Range("A1")
    .Value = "Test"
    .Characters(2, 2).Font.Color = vbGreen
 End With

 End Sub

小说明虽然不言而喻:第一个2"指的是需要着色的第一个字符,第二个2"指的是长度.

Small description although it speaks quite for itself: the first "2" refers to the first character that needs to be colored, the second "2" refers to the length.

这篇关于如何突出显示excel中的选定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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