Replace numbers using regular expressions

techiewithinme

New Member
Joined
Oct 4, 2011
Messages
7
Hello,

I have a date value in the below format and I want to replace all numbers after seconds part with blank. How do I do that using regular expression?

10/4/2011 01:10:11:000300000 AM

Thanks.
 
I was trying to replace thousands of date time values in one shot in Excel...QUOTE]

Sorry, this is not a RegExp solution, but it will still be quite fast in execution...

You can use this macro to do what you asked for...

Code:
Sub ReplaceJunkPartOfDateTimeText()
  Dim LastRow As Long, UnusedColumn As Long
  Const DataCol As Long = 1
  Const StartRow As Long = 1
  LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
  UnusedColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, _
                 SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column + 1
  With Cells(StartRow, UnusedColumn).Resize(LastRow - StartRow + 1)
    .FormulaR1C1 = "=REPLACE(SUBSTITUTE(TRIM(RC" & DataCol & "),"":"","":="",3),LEN(RC" & DataCol & ")-2,1,""=:"")"
    .Value = .Value
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).Clear
    On Error GoTo 0
    .Replace ":=*=:", "", xlPart
    Cells(StartRow, DataCol).Resize(LastRow - StartRow + 1).Value = .Value
    .Clear
  End With
End Sub
The one thing you have to do before running it is to change the DataCol and StartRow constants (declared in the two Const statements) to match your actual setup. DataCol is the column number where your existing date-time text is located and StartCol is the first row number in that column containing your data.
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I was trying to replace thousands of date time values in one shot in Excel...QUOTE]

Sorry, this is not a RegExp solution, but it will still be quite fast in execution...

You can use this macro to do what you asked for...

<<< rest of message snipped>>>
I forgot to mention in my previous message... after you run my macro, your data should now be real Excel dates which you can then format in whatever way you like.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,841
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top