【问题标题】:Find and format sub-string in an Excel cell在 Excel 单元格中查找和格式化子字符串
【发布时间】:2012-12-13 04:55:51
【问题描述】:

作为 Excel 编程的新手,我在网上搜索了一个函数/UDF/VBA,我可以使用它在 Excel 中找到一个单元格中的子字符串并在该单元格中加粗/着色。

我下面有这种表。 - A 列包含要部分格式化的文本, - B 列包含我正在寻找的子字符串, - C列包含子串(B)在串(A)中的位置

          A          B         C
1  ABCDEFGHIJKLM   FGHI      6-9
2  NOPQRSTUVWXYZ   UVWXY     8-12
...

这里是我需要获得的:

          A          B         C
1  ABCDE**FGHI**JKLM   FGHI      6-9
2  NOPQRST**UVWXY**Z   UVWXY     8-12
...

是否可以将其应用于整列而不是仅行。

任何帮助或建议将不胜感激。 谢谢!

【问题讨论】:

    标签: excel


    【解决方案1】:
    ' just add a loop and some checkings
    Sub t()
    
    Dim heightA As Long
    Dim heightB As Long
    Dim height As Long
    Dim i As Long
    
    heightA = Cells(Rows.Count, 1).End(xlUp).Row
    heightB = Cells(Rows.Count, 2).End(xlUp).Row
    If heightA < heightB Then
        height = heightA
    Else
        height = heightB
    End If
    
    If height > 1 Then
        For i = 1 To height
            Range("A" & i).Font.Bold = False
            ''Replace the formula with the full string
            Range("A" & i) = Range("A" & i).Value
            'Find the starting position of the string in B within the string produced by the formula
            If Range("B" & i).Value <> "" Then
                myPos = InStr(1, Range("A" & i).Value, Range("B" & i).Value, 1)
                If myPos > 0 Then
                    'Bold the string from B column
                    Range("A" & i).Characters(myPos, Len(Range("B" & i).Value)).Font.Bold = True
                End If
            End If
        Next i
    End If
    End Sub
    

    EDIT:在格式中使用 POSITION MASTER_STR、SUBSTR1、POS1(START-END)、SUBSTR2、POS2等

    Sub t()
    
    Dim heightA As Long
    Dim heightB As Long
    Dim height As Long
    Dim i As Long
    Dim length As Long
    Dim tmp
    Dim width As Long
    heightA = Cells(Rows.Count, 1).End(xlUp).Row
    heightB = Cells(Rows.Count, 2).End(xlUp).Row
    If heightA < heightB Then
        height = heightA
    Else
        height = heightB
    End If
    
    If height > 1 Then
        For i = 1 To height
            Range("A" & i).Font.Bold = False
            ''Replace the formula with the full string
            Range("A" & i) = Range("A" & i).Value
            'Find the starting position of the string in B within the string produced by the formula
            width = Cells(i, Columns.Count).End(xlToLeft).Column
            If width >= 3 Then
                For j = 3 To width Step 2
                If Cells(i, j).Value <> "" Then
                        tmp = Split(Cells(i, j).Value, "-")
                        myPos = CLng(tmp(0))
                        length = CLng(tmp(1)) - myPos + 1
                        'myPos = InStr(1, Range("A" & i).Value, Range("B" & i).Value, 1)
                        If myPos > 0 Then
                            'Bold the string from B column
                            Range("A" & i).Characters(myPos, length).Font.Bold = True
                        End If
                    End If
                Next j
            End If
        Next i
    End If
    End Sub
    

    示例输入

    ABCDEFG A   1-1 C   3-5
    ABCDEFG A   1-1 C   3-3
    

    【讨论】:

    • 太棒了,这简直太完美了!非常感谢拉里!
    • 如果您需要任何代码解释,您可以评论/聊天我
    • 我想是这样,但是如果 C、E、... 列及以后的列已经给出了位置,那么我们为什么需要 B、D、...列
    • 是的,对不起,我没有完全理解你的问题,所以现在你想保留你的格式,并且想要 A. 基于子字符串匹配,B. 基于给定的位置或者你要改变格式为 STRING,POSITION1,POSTITION2 ,... 或 STRING, SUBSTR1, SUBSTR2, ...
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多