VBA Excel網底色取代

  • 16777
  • 0
  • VBA
  • 2019-10-17

此應用來自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欄位的網底色,以此類推。

圖一:

圖二:

 

 

程式操作說明

Step1 :

Alt + F11 開啟 ExcelVBA 環境。

Step2 :

點選插入>模組(M)

Step3 :

貼上程式碼

 

====================程式碼開始======================

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 執行程式碼,點選執行

Step6:框選待取待區域

 

Step7:框選原始內容及條件達成內容

Step8:結果

參考網址:https://www.extendoffice.com/zh-TW/documents/excel/1873-excel-find-and-replace-multiple-values-at-once.html

以上。