首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel中文本字符串的格式列表

Excel中文本字符串的格式列表
EN

Stack Overflow用户
提问于 2016-09-26 16:38:49
回答 2查看 764关注 0票数 0

我正试图将该字体转换为红色,以显示excel中出现的单词列表。到目前为止,我能够找到一个单词,但我需要搜索一个完整的数组。我是VBA的新手,而且很努力。到目前为止,我已经找到了一个解决方案,但它处理的是找到一个字符串"F1":

代码语言:javascript
运行
复制
Sub test4String2color()
Dim strTest As String
Dim strLen As Integer
strTest = Range("F1")
For Each cell In Range("A1:D100")
If InStr(cell, strTest) > 0 Then
cell.Characters(InStr(cell, strTest), strLen).Font.Color = vbRed
End If
Next
End Sub

编辑:

我需要高亮显示的单元格以逗号分隔格式列出项目。例如,"Apple 1,Apple 3,Banana 4,Orange“。要搜索的值列表位于不同的单元格中,即"Apple“、"Banana 4”。我只想突出显示“香蕉4”,因为这与逗号分隔的值完全匹配。在目前的方案中,"Apple 1“或"Apple 4”的文本将被部分突出显示。

编辑2:

这是我工作簿中的实际格式:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-26 17:15:49

--这是一种通过循环遍历范围、集合和数组来实现所需内容的方法。

代码将在集合(所选的匹配词)和数组(每个单元格中分隔的单词字符串)之间找到匹配。如果找到匹配,则设置字符串中的起始字符和结束字符,并对这些值之间的字符进行着色。

代码语言:javascript
运行
复制
Sub ColorMatchingString()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
    Dim strTest As Collection: Set strTest = New Collection
    Dim udRange As Range: Set udRange = ws.Range("AC2:AC311") 'Define Search Ranges
    Dim myCell, myMatch, myString, i
    Dim temp() As String, tempLength As Integer, stringLength As Integer
    Dim startLength as Integer

    For Each myMatch In udRange 'Build the collection with Search Range Values
        strTest.Add myMatch.Value
    Next myMatch

    For Each myCell In ws.Range("A2:AB1125") 'Loop through each cell in range
        temp() = Split(myCell.Text, ", ") 'define our temp array as "," delimited
        startLength = 0
        stringLength = 0

        For i = 0 To UBound(temp) 'Loop through each item in temp array
            tempLength = Len(temp(i))
            stringLength = stringLength + tempLength + 2

            For Each myString In strTest
  'Below compares the temp array value to the collection value. If matched, color red.
                If StrComp(temp(i), myString, vbTextCompare) = 0 Then 
                    startLength = stringLength - tempLength - 1
                    myCell.Characters(startLength, tempLength).Font.Color = vbRed
                End If
            Next myString
        Next i
        Erase temp 'Always clear your array when it's defined in a loop
    Next myCell
End Sub
票数 2
EN

Stack Overflow用户

发布于 2016-09-26 17:31:29

为了与原始代码保持一致,您只需添加另一个For each cell in Range (以及其他一些东西):

代码语言:javascript
运行
复制
Sub test4String2color()
Dim wb As Workbook
Dim ws As Worksheet

Dim strLen  As Integer
Dim i       As Long
Dim tst As Range

Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet

Dim keyWordRng As Range
Dim dataRng As Range
Set keyWordRng = ws.Range("F1:F2")
Set dataRng = ws.Range("A1:A5")

For Each tst In keyWordRng
    Debug.Print "Searching for: " & tst
    For Each cell In dataRng
        If tst.Value = cell.Value Then
            cell.Characters(InStr(cell, tst), strLen).Font.Color = vbRed
        ElseIf InStr(1, cell.Value, ",") > 0 Then
            getWordsInCell cell, tst.Value
        End If
    Next cell
Next tst
End Sub


Sub getWordsInCell(ByVal cel As Range, keyword As String)
Dim words() As String
Dim keywordS As Integer, keywordE As Integer
words = Split(cel.Value, ",")

Dim i As Long
For i = LBound(words) To UBound(words)
    Debug.Print "Found multiple words - one of them is: " & words(i)
    If Trim(words(i)) = keyword Then
        keywordS = ActiveWorkbook.WorksheetFunction.Search(keyword, cel, 1)
        keywordE = ActiveWorkbook.WorksheetFunction.Search(",", cel, keywordS)
        cel.Characters(keywordS, (keywordE - keywordS)).Font.Color = vbRed
    End If
Next i

End Sub

请注意,我添加了范围(keyWordRngdataRng),您将需要调整您的工作表。这应该(祈祷)起作用了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39708050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档