delete data in cell after specific character

后端 未结 6 1961
無奈伤痛
無奈伤痛 2021-01-17 16:23

I have data in cells A1:A1000. It is a list of names followed by a small note, like this:

  • sam\" fast
  • nick\" long
  • tom\" quick
<
相关标签:
6条回答
  • 2021-01-17 16:32

    Easy! I don't know what version of Excel you are using, but in short you want to do a Convert Text to Columns and then split the cells using a delimiter of ". This will leave you with two columns, one of the data you want and one you can just delete.

    Here is the walk through in Office 2010:

    1. Highlight column A
    2. find the Data menu
    3. find the Convert Text to Columns menu
    4. Pick Delimited and hit next
    5. In the Other box, type "
    6. hit Finish

    Done! Now you have all your names in column A and you can just delete column B.

    To sum up, do a "Convert Text to Columns" and then split the cells using a delimiter of ". Super easy and fast.

    0 讨论(0)
  • 2021-01-17 16:33

    few options:

    Replace

    Range("A1:A1000").Replace """*", vbNullString
    

    If you require to manipulate the value further then the below are more appropriate:

    With Regex:

    Dim str As String, strClean As String
    Dim cell As Range
    
    For Each cell In Range("A1:A1000")
    
        With CreateObject("vbscript.regexp")
            .Pattern = "\""(.*)"
            .Global = True
            cell = .Replace(cell, vbNullString)
        End With
    
    Next cell
    

    Without Regex, splitting the string:

    Dim strSplit() As String
    Dim cell As Range
    
    For Each cell In Range("A1:A1000")
    
        If (cell.Value <> vbNullString) Then
    
            cell.Value = Split(cell.Value, """")(0)
    
        End If
    
    Next cell
    
    0 讨论(0)
  • 2021-01-17 16:51
    Sub Macro1()
        For Row = 1 To 1000
           S = Range("A" & Row).Cells.Value
           Pos = InStr(S, Chr(34))
           If Pos > 0 Then Range("A" & Row).Cells.Value = Left(S, Pos - 1)
        Next
    End Sub
    
    0 讨论(0)
  • 2021-01-17 16:52

    Here is a nifty trick without macros:

    Select the proper range (or even just click on A to select the entire column) and then do Ctrl+F, click Replace, in Find write exactly "* and leave the Replace with box empty. Now click Replace all and tada !

    It replaces everything after (and including) the quote with nothing because it uses * as a wildcard you left the replace box empty.

    Edit: As suggested here is the VBA code for this:

    Columns("A:A").Replace What:="""*", Replacement:="", LookAt:=xlPart
    
    0 讨论(0)
  • 2021-01-17 16:54

    In case you want to keep your source data, you can also do it with a simple Excel formula in the next column. Assuming that your data is in column A, the following formula will return only the name: =LEFT(A1,SEARCH("""",A1)-1)

    0 讨论(0)
  • 2021-01-17 16:54

    Press ctrl + f, click on replace tab, type * in the find what box and then click on replace all. No need to put anything in replace box. Here you are replacing everything after ..

    0 讨论(0)
提交回复
热议问题