简体   繁体   中英

Excel VBA - find keyword, then delete all content to the right

I'd like to include a subscript that finds "2014" and/or the variation '2 0 1 4' and delete all text to the right while keeping these 2 variations of 2014.

Bottom line - I'd like to keep the date and remove everything else to the right. I have another column in my spreadsheet that has these 'permit types'.

Here's is an example from the Column C in my spreadsheet:

05/07/2014 Electrical Permit Fee $174.58 $0.00

0 5/0 712 0 1 4 Electrical Permit Fee $23.83 $0.00

05/07/2014 Mechanical Permit Fee $102.00 $102.00

0512 9/2 01 4 Plumbing Permit Fee $50.00 $50.00

Here's a sample of the code I've been using:

Columns("C:C").Select
Selection.Replace What:="2014*:", Replacement_
:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False

Also, is there any way to fix these goofed up dates? My spreadsheet is a result of a PDF to Excel conversion....that's why the date is that way.

Thank you all so much for your help!

Try:

Selection.Replace "2014*", "2014"
Selection.Replace "2 0 1 4*", "2014"

And any other variations you might want. ie Replace 2014 and any text to the right of it, with just 2014.

EDIT: Adding a better solution (I think).

Also, if you want to remove all the other stuff, (1's instead of slashes) you could try something like this:

For Each cell In Range("C:C")
    Dim month As String
    Dim day As String
    Dim year As String
    If cell.Value <> "" Then
        cell.Replace " ", ""
        month = Left(cell.Value, 2)
        day = Mid(cell.Value, 4, 2)
        year = Mid(cell.Value, 7, 4)
        cell.Value = month & "/" & day & "/" & year
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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