VBA删除两个特定字符之间的string的子string

我正在格式化一列,其中一些值具有以下格式的不需要的数据:

XXXX(年)

我只想删除括号内和括号内的内容

我会用这个工作的正则expression式:

Sub DeleteMatches() Dim cell As Range, re As Object ' define the regular expression to match "(...)" ' Set re = CreateObject("VBScript.RegExp") re.Pattern = "\([^)]*\)" ' matches "(...)" ' ' iterate and clean each cell in range "C2:C100" ' For Each cell In Range("C2:C100") cell.Value = re.Replace(cell.Value, Empty) Next End Sub 

你可以很容易地用一个正则expression式来处理一个string中的多个replace

 Sub Test() Debug.Print CleanStr("xxxx(yyyy)") Debug.Print CleanStr("and me ()") Debug.Print CleanStr("and me ()` second string(aaa)") End Sub 

干净的string

 Function CleanStr(strIn As String) As String Dim objRegex As Object Set objRegex = CreateObject("vbscript.regexp") With objRegex .Pattern = "\([^)]*\)" .Global = True CleanStr = .Replace(strIn, vbNullString) End With End Function 

尝试这个:

 =LEFT(A1,FIND("(",A1)-1) 

select你想要处理的单元格并运行这个短的macros:

 Sub DataGrabber() Dim r As Range For Each r In Intersect(ActiveSheet.UsedRange, Selection) If InStr(1, r.Value, "(") > 0 Then r.Value = Split(r.Value, "(")(0) End If Next r End Sub 
  Dim x x = Split("xxxx(yyyy)", "(") Dim result result = x(0)