Board logo

標題: 請問vba要怎樣寫才能讓文字顏色交叉呈現@@ [打印本頁]

作者: s13030029    時間: 2020-12-21 09:36     標題: 請問vba要怎樣寫才能讓文字顏色交叉呈現@@

如題,要怎樣才能讓文字顏色如下圖的方式呈現
例如一開始用藍色,遇到下一個不一樣的名稱時,換另外一個顏色標示
用兩種顏色來標示,每個名稱的資料量不一定

[attach]32855[/attach]
作者: s13030029    時間: 2020-12-21 15:58

這是我目前想到的方法,如有高手有更好的方式歡迎指教~~~
  1. Sub color()
  2.     color1 = -3902908
  3.     color2 = -13270700
  4.     With ActiveSheet
  5.         r = .Columns(1).Find(what:="*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row
  6.         c = color1
  7.         CC = 1
  8.         .Cells(1, "A").Font.color = c
  9.         For i = 1 To r
  10.             a = .Cells(i, "A")
  11.             If i = 1 Then
  12.                 b = .Cells(i + 1, "A")
  13.             Else
  14.                 b = .Cells(i - 1, "A")
  15.             End If
  16.             If a = b Then
  17.                 .Cells(i, "A").Font.color = c
  18.             ElseIf a <> b And CC = 1 Then
  19.                 c = color2
  20.                 CC = 0
  21.                 .Cells(i, "A").Font.color = c
  22.             ElseIf a <> b And CC = 0 Then
  23.                 c = color1
  24.                 CC = 1
  25.                 .Cells(i, "A").Font.color = c
  26.             End If
  27.         Next i
  28.     End With
  29. End Sub
複製代碼

作者: 准提部林    時間: 2020-12-21 19:59

Sub TEST()
Dim xR As Range, xH As Range, T$, Cr, c%
[A:A].Font.ColorIndex = 1
Cr = Array(5, 3)
For Each xR In Range([A1], [A65536].End(xlUp))
    If xR & "" <> T Then Set xH = xR: T = xR & ""
    If xR(2) & "" <> T Then
       Range(xH, xR).Font.ColorIndex = Cr(c)
       c = (c + 1) Mod 2
    End If
Next
End Sub
作者: hcm19522    時間: 2020-12-22 09:59

https://blog.xuite.net/hcm19522/twblog/589522000
作者: y54161212    時間: 2020-12-23 12:19

Sub TEST()
Dim xR As Range, xH As Range, T$, Cr, c%
[A:A].Font.ColorIndex = 1
Cr = Array(5, 3)
F ...
准提部林 發表於 2020-12-21 19:59


版大的宣告
T$ c%
但在程式內只有 T c
請問這附帶的符號 $ %在宣告內有什麼作用嗎?
作者: 准提部林    時間: 2020-12-23 14:34

回復 5# y54161212

可自行去查"變數型態" 的相關說明...EXCEL中的說明檔 或 GOOGLE
作者: y54161212    時間: 2020-12-23 15:08

回復  y54161212

可自行去查"變數型態" 的相關說明...EXCEL中的說明檔 或 GOOGLE
准提部林 發表於 2020-12-23 14:34


終於找到了!
類型的簡略表示法為:% -integer; & -long; @ -currency; # -double; ! -single; $ -string
作者: n7822123    時間: 2020-12-24 03:34

回復 1# s13030029

我來提供一個 "有趣" 的寫法

Sub Test1224()
    Dim Cor: Cor = Array(10, 5)
    Range([A1], [A1].End(4)).Select
    On Error Resume Next
    Do Until Err <> 0
      K% = K% + 1: Selection.Font.ColorIndex = Cor(K Mod 2)
      Selection.ColumnDifferences(ActiveCell).Select
    Loop: [A1].Activate
End Sub

作者: Kubi    時間: 2020-12-25 21:30

回復 1# s13030029
請參考
Sub test()
    Dim mycolor%, x$, cell As Range
    For Each cell In Range([A1], [A65536].End(3))
        If cell.Value <> x Then
            mycolor = IIf(mycolor = 5, 10, 5)
            x = cell.Value
        End If
        cell.Font.ColorIndex = mycolor
    Next cell
End Sub




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)