【教學】Excel VBA 如何使用Replace取代字串?超簡單馬上學會!

excel vba 取代字串 尋找取代 replace用法

excel vba 取代字串 尋找取代 replace用法

Step 1 在功能列「開發人員」中程式碼選擇「Visual Basic」


Step 2 複製下方程式碼貼上,並按下執行即可,其中What為原始字串,Replacement為新替換字串,LookAt為是否要精準匹配(xlWhole)或是部分匹配(xlPart),MatchCase為是否要區分大小寫

Sub ReplaceText()
    ' 將工作表中的 "old" 替換為 "new"
    Cells.Replace What:="old", Replacement:="new", LookAt:=xlPart, MatchCase:=False
End Sub

excel vba 取代字串 尋找取代 replace用法

Step 3 若要修改取代範圍,可將程式碼中的Cells更改為Range(“範圍”)

Sub ReplaceText()
    ' 將工作表中的 "old" 替換為 "new"
    Range("A:A").Replace What:="old", Replacement:="new", LookAt:=xlPart, MatchCase:=False
End Sub
【教學】Excel VBA 如何使用Replace取代字串?超簡單馬上學會!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Scroll to top