此應用來自https://www.extendoffice.com/zh-TW/documents/excel/1873-excel-find-and-replace-multiple-values-at-once.html 此文章"如何在Excel中一次查找和替換多個值?"中的衍生,隨手紀錄一下。
編譯軟體 Excel VBA
重點應用:Range 變數、以及 利用Offset() 函式 取得Range.Cells 中Cell的內容文字。
條件:
如果A欄位內容文字等於D欄位內容文字,D欄位的網底顏色將被取代為B欄位網底顏色。
Ex : 例如 A2 =D2 的文字內容相等,此時 D2的網底色會變成B欄位的網底色,以此類推。
圖一:
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570612938_39079.png)
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570612957_61069.png)
圖二:
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570612976_34508.png)
程式操作說明
Step1 :
Alt + F11 開啟 ExcelVBA 環境。
Step2 :
點選插入>模組(M)
Step3 :
貼上程式碼
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570613005_59735.png)
====================程式碼開始======================
Sub MultiFindNReplace()
'Update 20191015
Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each InRng In InputRng.Columns.Cells
For Each Rng In ReplaceRng.Columns(1).Cells
If InRng.Value = Rng.Value Then
'取代網底
InRng.Interior.Color = Rng.Offset(0, 1).Interior.Color
End If
Next
Next
Application.ScreenUpdating = True
End Sub
====================程式碼結束======================
Step 5:按下 F5 執行程式碼,點選執行
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570613028_57711.png)
Step6:框選待取待區域
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570613035_19347.png)
Step7:框選原始內容及條件達成內容
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570613053_52209.png)
Step8:結果
/a30d0f06-14cf-4e99-8ec5-2372c39c5dd3/1570613060_5319.png)
以上。