excel 检测单元格中的粗体文本并在这些标题后添加“Total”

gcuhipw9  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(90)

我希望所有粗体的标题都在文本后面加上“总计”。我不知道为什么这个公式不起作用。
之前-这是什么样子x1c 0d1x
我希望它看起来像这样

这是我做的公式,我不知道是什么原因造成的:

Sub Test()
    Dim rng As Range
    Dim cell As Range
    Dim i As Integer
    Dim text As String
    
    Set rng = ws.Range("C1:C2000")
    
    For Each cell In rng
        If Not IsEmpty(cell.Value) And cell.Font.Bold Then
            
            For i = LBound(words) To UBound(words)
                If cell.Characters(Start:=InStr(cell.Value, words(i)), Length:=Len(words(i))).Font.Bold Then
                    words(i) = words(i) & " Total"
        
End Sub

字符串

kkbh8khc

kkbh8khc1#

我试了一下,效果很好

Sub BoldToCell()
        Dim ws As Worksheet
        Dim cell As Range
        Dim lastRow As Long
        
        
        Set ws = ThisWorkbook.Worksheets("Sheet1") 
        
        
        lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
        
        
        For Each cell In ws.Range("B1:B" & lastRow)
            
            If cell.Font.Bold = True Then
                
                cell.Value = cell.Value & " Total"
            End If
        Next cell
    End Sub

字符串

oyxsuwqo

oyxsuwqo2#

  • 你不需要嵌套的For循环
  • words没有被赋值。使用Option Explicit可以帮助避免这种类型的错误。
Option Explicit

Sub Test()
    Dim rng As Range
    Dim cell As Range
    Dim ws As Worksheet
    Set ws = ActiveSheet 'modify as needed
    Set rng = ws.Range("C1", ws.Cells(ws.Rows.Count, 3).End(xlUp))
    For Each cell In rng
        If Not IsEmpty(cell.Value) And cell.Font.Bold Then
            cell.Value = cell.Value & " Total"
        End If
    Next
End Sub

字符串

相关问题