Bolding a specific part of cell

前端 未结 4 2163
梦毁少年i
梦毁少年i 2021-02-13 20:39

I have a cell that is referenced as =\"Dealer: \" & CustomerName. CustomerName is a dictionary referenced name. How could I go along of bolding only \"Dealer:\"

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-13 20:59

    As they already told you can't format a partial cell value if this latter derives from a formula/function in that same cell

    However there may be some workarounds that may suit your needs

    Unluckily I can't actually grasp your real environment so here are some blind shots:


    1st "environment"

    You have a VBA code running that at some point writes in a cell like:

    Cells(5, 1).Formula = "=""Dealer: "" & CustomerName"
    

    and you want to have the "Dealer:" part bold

    • the most straightforward way would then be

      With Cells(5, 1)
          .Formula = "=""Dealer: "" & CustomerName"
          .Value = .Value
          .Characters(1, 7).Font.Bold = True
      End With
      
    • but you could also use the Worksheet_Change() event handler as follows:

      your VBA code is only

      Cells(5, 1).Formula = "=""Dealer: "" & CustomerName"
      

      while placing the following code in the relevant worksheet code pane:

      Private Sub Worksheet_Change(ByVal Target As Range)
          With Target
              If Left(.Text, 7) = "Dealer:" Then
                  Application.EnableEvents = False '<-- prevent this macro to be fired again and again by the statement following in two rows
                  On Error GoTo ExitSub
                  .Value = .Value
                  .Characters(1, 7).Font.Bold = True
              End If
          End With
      
      ExitSub:
          Application.EnableEvents = True '<-- get standard event handling back
      End Sub
      

      where On Error GoTo ExitSub and ExitSub: Application.EnableEvents = True shouldn't be necessary, but I left them as a good practice when Application.EnableEvents = False id used


    2nd "environment"

    You have cell(s) in your excel worksheet containing a formula, like:

    ="Dealer:" & CustomerName
    

    where CustomerName is a named range

    and your VBA code is going to modify the content of that named range

    in this case the Worksheet_Change() sub would be triggered by the named range value change and not by the cell containing the formula

    so I'd go checking if the changed cell is a valid one (i.e. corresponding to a well known named range) and then go with a sub that scans a predefined range and finds and format all cells with formulas that use that `named range, like follows (comments should help you):

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Target
            If Not Intersect(ActiveWorkbook.Names("CustomerName").RefersToRange, Target) Is Nothing Then
                Application.EnableEvents = False '<-- prevent this macro to be fired again and again by the statement following in two rows
                On Error GoTo ExitSub
                FormatCells Columns(1), "CustomerName" '<-- call a specific sub that will properly format all cells of passed range that contains reference to passed "named range" name
            End If
        End With
    
    ExitSub:
        Application.EnableEvents = True '<-- get standard event handling back
    End Sub
    
    Sub FormatCells(rng As Range, strngInFormula As String)
        Dim f As Range
        Dim firstAddress As String
    
        With rng.SpecialCells(xlCellTypeFormulas) '<--| reference passed range cells containg formulas only
            Set f = .Find(what:=strngInFormula, LookIn:=xlFormulas, lookat:=xlPart) '<--| search for the first cell in the referenced range containing the passed formula part
            If Not f Is Nothing Then '<--| if found
                firstAddress = f.Address '<--| store first found cell address
                Do '<--| start looping through all possible matching criteria cells
                    f.Value = f.Value '<--| change current cell content into text resulting from its formula
                    f.Characters(1, 7).Font.Bold = True '<--| make its first 7 characters bold
                    Set f = .FindNext(f) '<--| search for next matching cell
                Loop While f.Address <> firstAddress '<--| exit loop before 'Find()' method wraps back to the first cell found
            End If
        End With
    End Sub
    

提交回复
热议问题