How to remove characters from the right of a space for all cells in specific columns

Gay Begnell 1 Reputation point
2021-06-08T01:57:48.813+00:00

Sorry if this is a stupid question, but I am new to vba and I am trying to remove characters from the right of a space

an example of the text in the cell is - 5/08/2015 at 12:22 PM

I want to reduce it to 5/08/2015 (d/mm/yyyy)

cell is formatted as Date (d/mm/yyyy)

I have come up with:

Dim tmp As String
Dim DateOnly As String
tmp = Range("E3").Value
DateOnly = Left(tmp, InStr(tmp, " ") - 1)
Range("E3").Value = DateOnly

this removes the time from the end of the cell, but it converts the date to 8/05/2015

I am also struggling to get it to work on all cells within 3 columns ("E:G")

Can anyone assist please?

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2021-06-08T05:58:42.76+00:00

    To solve the first problem, try this modification:

    Range("E3").Value = CDate(DateOnly)


  2. Frederick Rothstein 1 Reputation point
    2021-06-11T21:22:55.323+00:00

    To handle all three columns (starting on Row 3 downward)...

    Range("E3:G" & Rows.Count).Replace " *", "", xlPart, , , False, False

    0 comments No comments