Date format to dd-mm-yyyy in VBA code

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
53
Hello,

Despite the below standing code (Private Sub Process_XLS_Files) copies a date like 2-1-2017 it turns it into 1-2-2017. I have tried to get it fixed by add in an extra piece of code, but that did not help. Both with function of Dateformat as Numberformat.

Look forward to your reply. Hans Grandia NL

Code:
 'Date format to "dd-mm-yyyy"
    Sheets(1).Delete 'voorblad
    For Each wksht In Workbooks
    Rowcounter = Application.CountA(Range("A:A"))
    Range("A2", "A & Rowcounter").NumberFormat = "dd-mm-yyyy"
    Next wksht

Code:
Private Sub Process_XLS_Files(Fso As Scripting.FileSystemObject, folderPath As String)
       
    Dim Folder As Scripting.Folder, Subfolder As Scripting.Folder, File As Scripting.File
    Dim wksht As Worksheet
    Dim Masterfile As Workbook
    Dim Rowcounter As Long
        
    Set Folder = Fso.GetFolder(folderPath)
    
    For Each wksht In ActiveWorkbook.Worksheets
    For Each Subfolder In Folder.SubFolders
        For Each File In Subfolder.Files
            If InStr(File.Name, ".xls") Then
                Workbooks.Open (File.Path)
                    On Error Resume Next
                        Worksheets(wksht.Name).Select
                        If Err.Number = 0 Then
                            Range(Range("A14"), Range("A14").End(xlDown)).Select
                            ActiveCell.CurrentRegion.Select
                            Selection.Copy
                            ActiveWorkbook.Close
                            Worksheets(wksht.Name).Activate
                            Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
                            ActiveSheet.Paste
                         Else
                         On Error GoTo 0
                         ActiveWorkbook.Close
                        End If
            End If
        Next
    Next
    Next wksht
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try
Code:
    Range("A2:A" & Rowcounter).NumberFormat = "dd-mm-yyyy"
 
Upvote 0
Changing the format of a cell does not change its actual value, it just changes how it is presented.
If the data is incorrect, you will actually need to convert it (and the number formatting will not do that).

Are you, perchance, using a European version of Excel?

Also note, in your loop, since you are not selecting each sheet as you loop through them, nor are you including the sheet reference in your range references, all the code is only being applied to the active sheet, and not each individual sheet. You either need to select each sheet as you loop through it or include the sheet reference in with your range reference. You also have a few other issues with how you wrote that loop. Here is it, cleaned up:
Code:
    For Each wksht In [COLOR=#ff0000]Worksheets[/COLOR]
        Rowcounter = [COLOR=#ff0000]wksht.Cells(Rows.Count, "A").End(xlUp).Row[/COLOR]
        [COLOR=#ff0000]wksht[/COLOR].Range([COLOR=#ff0000]"A2:A" & Rowcounter[/COLOR]).NumberFormat = "dd-mm-yyyy"
    Next wksht
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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