WorksheetFunction.Replace 方法 (Excel)

根據您指定的字元數,以不同的文字字串取代部分文字字串。

語法

運算式。取代 (Arg1、 Arg2、 Arg3、 Arg4)

運算式 代表 WorksheetFunction 物件的變數。

參數

名稱 必要/選用 資料類型 描述
Arg1 必要 字串 您想要取代其中某些字元的文字。
Arg2 必要 雙精確度 在 Arg1 中您想要以 Arg4 取代的字元位置。
Arg3 必要 雙精確度 您希望 Replace 方法在 Arg1 中以 Arg4 取代的字元數目。
Arg4 必要 字串 要取代 Arg1 中字元的文字。

傳回值

字 串 值,代表取代後的新字串。

範例

這個範例會用 ac-ef 取代 abcdef,並且在此程序進行期間通知使用者。

Sub UseReplace() 
 
 Dim strCurrent As String 
 Dim strReplaced As String 
 
 strCurrent = "abcdef" 
 
 ' Notify user and display current string. 
 MsgBox "The current string is: " & strCurrent 
 
 ' Replace "cd" with "-". 
 strReplaced = Application.WorksheetFunction.Replace _ 
 (Arg1:=strCurrent, Arg2:=3, _ 
 Arg3:=2, Arg4:="-") 
 
 ' Notify user and display replaced string. 
 MsgBox "The replaced string is: " & strReplaced 
 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。