iT邦幫忙

0

請問excel vba如何用關鍵字來指定儲存格顏色?

  • 分享至 

  • xImage

請問excel vba如何用關鍵字來指定儲存格顏色(大概4000筆資料),因為資料都是一批一批新的所以不適合用"格式化"謝謝
假設:2020、2011、2010、korea、english、南 是關鍵字

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2022-03-16 22:56:19
最佳解答

不待招式用老

Sub Main()
    Range("A1").Select
    Do While ActiveCell.Value <> ""
        If hasKeyword(ActiveCell.Value) Then
            With Selection.Interior
                .ColorIndex = 6
                .Pattern = xlSolid
            End With
        Else
            Selection.Interior.ColorIndex = xlNone
        End If
        ActiveCell.Offset(1, 0).Select
    Loop
End Sub
Function hasKeyword(ByVal str As String) As Boolean
    If str = "2020" Or str = "2011" Or str = "2010" Or str = "korea" Or str = "english" Or str = "南" Then
        hasKeyword = True
        Exit Function
    End If
    hasKeyword = False
End Function

/images/emoticon/emoticon73.gif

xyz321763 iT邦新手 5 級 ‧ 2022-03-17 01:39:07 檢舉

不好意思!它顯示語法錯誤,請問怎麼改?(office 2019)
另外問一下"不待招式用老"的意思?我只查到射雕之楊康列傳,但左看右看還是看不懂意思:D

1.改好了(不好意思先前沒測就上,我自己太靠勢了)
2.不待招式用老只是口語,沒什麼深究的意思(其實你應該有發現,這問題和先前的問題類似,只有差在判斷「韓文」和「特定文字」而已)

xyz321763 iT邦新手 5 級 ‧ 2022-03-17 12:59:53 檢舉

可以用了!再次感謝你!能花時間幫我就非常感謝你了!

我功力不夠,目前還只是學到皮毛(還在極度緩慢的學習中),我有覺得上一個程式應該能用,但是我嘗試改動後發現我是個災難:DD

0
paicheng0111
iT邦大師 5 級 ‧ 2022-03-17 09:12:49

條件式格式設定
https://ithelp.ithome.com.tw/upload/images/20220317/20108797ycRIxsJX7W.png

blanksoul12 iT邦研究生 5 級 ‧ 2022-03-17 17:19:49 檢舉

如果只是着色,條件格式可以了吧

xyz321763 iT邦新手 5 級 ‧ 2022-03-18 10:09:38 檢舉

不好意思 因為每次都是新文件,所以巨集比較方便,謝謝你

0
ccenjor
iT邦高手 1 級 ‧ 2022-03-17 19:21:41

先在D欄打好要黃底的關鍵字。
將A欄資料框選起來。
再點取「常用 > 條件式格式設定 > 新增規則」指令。
格式設定如下:
https://ithelp.ithome.com.tw/upload/images/20220317/20109881YaSIoP19uG.png
結果如下:
https://ithelp.ithome.com.tw/upload/images/20220317/20109881jI1rpYqHDd.png

xyz321763 iT邦新手 5 級 ‧ 2022-03-18 10:10:00 檢舉

不好意思 因為每次都是新文件,所以巨集比較方便,謝謝你

我要發表回答

立即登入回答