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

时间:2016-05-20 17:40:13

标签: excel vba excel-vba

我正在格式化一个列,其中某些值包含以下格式的不需要的数据:

  

XXXX(YYYY)

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

5 个答案:

答案 0 :(得分:3)

我会使用正则表达式来完成这项工作:

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

答案 1 :(得分:3)

您可以使用 Regexp

轻松地在一个字符串中进行多次替换
Sub Test()
Debug.Print CleanStr("xxxx(yyyy)")
Debug.Print CleanStr("and me ()")
Debug.Print CleanStr("and me ()` second string(aaa)")
End Sub

清洁字符串

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

答案 2 :(得分:1)

试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>
<script src="http://maps.google.com/maps/api/js"></script>
<div class="container">
  <div class="well">
    <div id="map_canvas"></div>
  </div>
</div>

答案 3 :(得分:1)

选择您要处理的单元格并运行此短宏:

win32file.QueryDosDevice

答案 4 :(得分:0)

Dim x
x = Split("xxxx(yyyy)", "(")
Dim result
result = x(0)