簡體   English   中英

更改Excel單元格中的特定數值

[英]Changing specific numeric values in an excel cell

我想使用VBA宏將字符串“ 1001001001”中的1和0着色為兩種不同的顏色。 這可能嗎? 這是我嘗試的代碼

Sub changeTextColor()

     Green = RGB(0, 255, 0)
     Red = RGB(255, 0, 0)
     Black = RGB(0, 0, 0)

     'Select cell
     Range("H2").Select

     For x = 1 To 10
         If ActiveCell.Value = "1" Then

            'Change the text color if "1"

             ActiveCell.Font.Color = Green   

         ElseIf ActiveCell.Value = "0" Then

             'Change the text color if "0"

             ActiveCell.Font.Color = Red

         End If

         ActiveCell.Offset(1, 0).Select
     Next
 End Sub

像這樣:

Sub changeTextColor()

    Dim v, s, clr, rng, x

    Set rng = Range("H2")


    Do While rng.Value <> ""
        v = rng.Value
        For x = 1 To Len(v)

            Select Case Mid(v, x, 1)
               Case "1": clr = vbGreen
               Case "0": clr = vbRed
               Case Else: clr = vbBlack
            End Select

            rng.Characters(x, 1).Font.Color = clr

        Next x

        Set rng = rng.Offset(1, 0)
    Loop
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM