2016-01-23 162 views
1

我有一個電子表格,其中一列有字符串數據(名稱),後面跟着括號中的數字;即(9815536​​)。這些數字的長度不是固定的。我需要擺脫括號和括號中的數字。我試過使用Columns()。Cells.Replace funtion to no svn。有沒有辦法使用正則表達式來做到這一點?小區例子看起來像:需要刪除括號和括號中的數字

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

我已經試過代碼如下所示:

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

你可以用一個簡單的公式'= TRIM(LEFT(A1,FIND( 「(」,A1)-1))' –

+0

或環通過vba中的列並使用'cell = trim(split(cell,「(」)(0))''cell'是參考。 –

+0

如果你真的想使用正則表達式,那麼你需要看看[這裏]( http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops)正確使用。 –

回答

1

實現一個快速的方法是使用

  • 一個
  • 和VBA中的變體數組。

    使用基於我的產品代碼Using Variant Arrays in Excel VBA for Large Scale Data Manipulation

    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