2015-10-13 75 views

回答

1

要使用VBA解決方案執行此操作,您仍然必須複製/粘貼特殊值>,因爲如果該單元格是公式,您不能僅着色單元格的某些字符。

的收官手動步驟進入VBA子程序,我們將有:

  1. 複製從公式計算出的值,並將其粘貼到一個新的細胞。
  2. 確定文本「Item:」的起始位置及其包含的字符數
  3. 確定所需顏色的顏色值。

(假設你的公式爲D1),它看起來像:

Sub avgSumColor() 
    Dim rngFormulaCell As Range, rngValueCell As Range 
    Dim itemStart As Integer, itemLength As Integer 

    'Set the cell with the formula and the cell where we'll put the value 
    Set rngFormulaCell = Sheet1.Range("D1") 
    Set rngValueCell = Sheet1.Range("D2") 

    'Copy/Paste special (just set the value of the valuecell to the value of the formulacell) 
    rngValueCell.Value = rngFormulaCell.Value 

    'Figure out where "Item:<number>" starts and 
    ' how many characters long it is 
    itemStart = InStr(1, rngValueCell.Value, "Items") 
    itemLength = Len(rngValueCell.Value) + 1 - itemStart 

    'set the first bit up to the text "Item:<number>" to red 
    rngValueCell.Characters(1, itemStart).Font.Color = RGB(255, 0, 0) 

    'set the last bit containing "Item:<number>" to black 
    rngValueCell.Characters(itemStart, itemLength).Font.Color = RGB(1, 1, 1) 
End Sub 
相關問題