Excel VBA 移除字串

2007-09-08 8:02 am
請問怎樣用 Excel VBA 移除以下每個cell 內的(.ab) 字串~


333.ab
5323.ab
44.ab
63622.ab

Thank you!

回答 (2)

2007-09-08 7:38 pm
✔ 最佳答案




可以用這個函數(function),它不但可在VBA中使用,也可當Worksheet Function來用:








Public Function RTrimStr(ByRef stSource As String, ByRef stTarget As String) As String
If InStr(stSource, stTarget) > 0 Then
RTrimStr = Left(stSource, InStr(stSource, stTarget) - 1)
Else
RTrimStr = stSource
End If
End Function






使用範例







VBA:

[A1] = RTrimStr([A1], ".ab")

Worksheet Function:

=RTRIMSTR(A1, ".ab")





參考: 個人經驗
2007-09-17 6:41 am
建議用 Val 或 Replace

Function valx(numstr As String) As Double
valx = Val(numstr)
End Function

Function valy(numstr As String, str As String) As String
valy = Replace(numstr, str, "")
End Function

如希望傳回值是數字, 宣告為 Double,
若傳回文本, 則宣告為 String
任君選擇


收錄日期: 2021-05-03 01:38:58
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070908000051KK00011

檢視 Wayback Machine 備份