Simple VBA for dates is driving me crazy

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
My workbook has some imported data in it, currently manually pasted.

One of the columns is a date field, but the source data can vary in format and either be what should be DD.MM.YYYY or DD/MM/YYYY depending on the source spreadsheet.

As I need to work with the dates, I wrote a macro to replace the "." with "/" and then format as short date.

The incoming data should be in "General Format"

I wrote this:

VBA Code:
        .Columns("B").NumberFormat = "General"
        .Columns("B").Replace ".", "/", xlPart, xlByRows, True
        .Columns("B").NumberFormat = "dd/mm/yyyy"

I think the first line is now redundant.

If the data has "/" separators then everything works ok.
But if it is "." then the program switches it from dd.mm to mm/dd for no reason that I fathom.

Any tips?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Why not use Text to columns, that way you shouldn't have a problem
 
Upvote 0
Thanks Fluff.

I now have the opposite problem!

If the data has a "/" separator it now converts it into "mm/dd/yyyy"!

Do I need to put in an IF statement to distinguish between the import methods?
 
Upvote 0
As long as you select the date format the dates are in (not the format you want) it should be ok
 
Upvote 0
This must be something with the way I am using the code.

When I do it manually, it works just fine.

However when I use recorded Macro code:

VBA Code:
        .Range(.Cells(2, 2), .Cells(LRow, 2)).TextToColumns Destination:=Range("B2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 4), TrailingMinusNumbers:=True

I get the mix up with the data if it comes in with "/" instead of "."

I used Month() function to check and it has converted the cell "04/01/2018" into 1st April 2018 for sure.

I am guessing the macro code is wrong?
 
Upvote 0
You're quite right it does mess it up with the recorded macro, but works manually. Never had dates in differing formats, so didn't realise that.
I think your only option will be to loop through the data & convert individually.
 
Upvote 0
DateIssueMrExcel 20200224.png


Why not tear it apart with left & right formulas? I put it into YYYYMMDD
format because you can sort them and bypass Excel's date function which
I hate with a passion.
 
Upvote 0
Rather than looping through the range, try
VBA Code:
Sub TheRedCardinal()
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(isnumber(@),@,date(right(@,4),mid(@,4,2),left(@,2)))", "@", .Address))
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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