简体   繁体   English

需要删除括号和括号中的数字

[英]Need to remove numbers in parentheses and parentheses

I have a spreadsheet in which a column has string data (names) followed by numbers in parentheses; 我有一个电子表格,其中一列包含字符串数据(名称),后跟括号中的数字; ie, (9815536). 即(9815536​​)。 These numbers are not constant in length. 这些数字的长度不是恒定的。 I need to get rid of the numbers in the parentheses and the parentheses. 我需要删除括号和括号中的数字。 I've tried using Columns().Cells.Replace funtion to no avail. 我试过使用Columns()。Cells.Replace函数无济于事。 Is there a way to use a regular expression to do this? 有没有办法使用正则表达式来做到这一点? A cell example would look like: 一个单元格示例如下所示:

Column A
John Doe (9815536)
Sam Smith (12906)
...

Code I've tried looks like: 我尝试过的代码如下所示:

Columns("A:A").Select
    Selecton.Replace What:="\([0-9]*\), _
    Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, _
    MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

A quick way to implement this is to use 一种快速的实现方法是使用

  • a 一个正则
  • and a variant array in VBA. 和VBA中的变体数组。

    Using code based on my Article Using Variant Arrays in Excel VBA for Large Scale Data Manipulation 使用基于我的文章的代码在Excel VBA中使用变体数组进行大规模数据处理

     Sub KillNumParen() Dim rng1 As Range Dim rngArea As Range Dim lngRow As Long Dim lngCol As Long Dim lngCalc As Long Dim objReg As Object Dim X() On Error Resume Next Set rng1 = Application.InputBox("Select range for the replacement of non-number", "User select", Selection.Address, , , , , 8) If rng1 Is Nothing Then Exit Sub On Error GoTo 0 'See Patrick Matthews excellent article on using Regular Expressions with VBA Set objReg = CreateObject("vbscript.regexp") objReg.Pattern = "\\(\\d+\\)" objReg.Global = True 'Speed up the code by turning off screenupdating and setting calculation to manual 'Disable any code events that may occur when writing to cells With Application lngCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual .EnableEvents = False End With 'Test each area in the user selected range 'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on For Each rngArea In rng1.Areas 'The most common outcome is used for the True outcome to optimise code speed If rngArea.Cells.Count > 1 Then 'If there is more than once cell then set the variant array to the dimensions of the range area 'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks X = rngArea.Value2 For lngRow = 1 To rngArea.Rows.Count For lngCol = 1 To rngArea.Columns.Count 'replace the leading zeroes X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString) Next lngCol Next lngRow 'Dump the updated array sans leading zeroes back over the initial range rngArea.Value2 = X Else 'caters for a single cell range area. No variant array required rngArea.Value = objReg.Replace(rngArea.Value, vbNullString) End If Next rngArea 'cleanup the Application settings With Application .ScreenUpdating = True .Calculation = lngCalc .EnableEvents = True End With Set objReg = Nothing End Sub 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM