Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6353851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:28:57+00:00 2026-05-24T22:28:57+00:00

I’ve got a date column that contains dates in mixed format. For example: A

  • 0

I’ve got a date column that contains dates in mixed format. For example:

A

21.03.1990

03/21/1990

So, basically there are two different formats in one column: dd.mm.yyyy and mm/dd/yyyy. I’m trying to write a VBA script to change format of all dates in the column to be yyyy-mm-dd. That’s what I’ve got so far:

Sub changeFormat()

Dim rLastCell As Range
Dim cell As Range, i As Long
Dim LValue As String

i = 1

With ActiveWorkbook.Worksheets("Sheet1")
    Set rLastCell = .Range("A65536").End(xlUp)
    On Error Resume Next
    For Each cell In .Range("A1:A" & rLastCell.Row)
        LValue = Format(cell.Value, "yyyy-mm-dd")
        .Range("B" & i).Value = LValue
        i = i + 1
    Next cell
    On Error GoTo 0
End With

End Sub

I know that it’s not elegant piece of code, but I’m beginner with VBA so please forgive me.
The problem with that code is that it just rewrite unchanged A column into column B, when I change argument in Format function from yyyy-mm-dd to dd/mm/yyyy it works, but only for dates in format mm/dd/yyyy, and leaves dd.mm.yyyy untouched. I would appreciate any advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:28:58+00:00Added an answer on May 24, 2026 at 10:28 pm

    UPDATED: NEW ANSWER

    Here is a solution that will do the job! The sub routine includes a function that does the replacement (the function itself is really useful!). Run the sub and all occurances in column A will be fixed.

    Sub FixDates()
    
    Dim cell As range
    Dim lastRow As Long
    
    lastRow = range("A" & Rows.count).End(xlUp).Row
    
    For Each cell In range("A1:A" & lastRow)
        If InStr(cell.Value, ".") <> 0 Then
            cell.Value = RegexReplace(cell.Value, _
            "(\d{2})\.(\d{2})\.(\d{4})", "$3-$2-$1")
        End If
        If InStr(cell.Value, "/") <> 0 Then
            cell.Value = RegexReplace(cell.Value, _
            "(\d{2})/(\d{2})/(\d{4})", "$3-$1-$2")
        End If
        cell.NumberFormat = "yyyy-mm-d;@"
    Next
    
    End Sub 
    

    Place this function in the same module:

    Function RegexReplace(ByVal text As String, _
                          ByVal replace_what As String, _
                          ByVal replace_with As String) As String
    
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    
    RE.pattern = replace_what
    RE.Global = True
    RegexReplace = RE.Replace(text, replace_with)
    
    End Function
    

    How it works: I have a nifty RegexReplace function that allows you to do replace using regular expressions. The sub mearly loops through your A column and does a regex replace for those 2 cases you mentioned. The reason I use an Instr() first is to determain if it needs the replacement, and which kind. You could technically skip this but doing replace on cells that don’t need it is really costly. At the end I format the cell to your custom date format regardless of what’s inside for safe measure.

    In case you aren’t familiar with Regex (for ref: http://www.regular-expressions.info/), the expression I am using is:

    • Each item in () are capture groups – aka, the stuff you want to mess with
    • \d stands for a number [0-9].
    • {2} means 2 of, and {4} mean 4 of. I have been explicit here for safety.
    • The \ before the . in the first replace is needed since “.” has special meaning.
    • In VBA regex, you refer to capture groups by using $ + no. of group. This is how I flip the order of the 3 items.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.