VBA Format function won't work

pirdop

Board Regular
Joined
Jul 27, 2010
Messages
72
Hello all,
Any ideas why the Format function won’t work?

I collect a date via an userform and when I try to output it to a spread sheet it changes the format randomly.
I tried to use unsuccessfully the format function to correct this.

Code:
 With ws
                        .Cells(iRow, 2).Value = Format(Me.DateBox.Value, "dd/mm/yyyy")
                            .Cells(iRow, 2) = Format(.Cells(iRow, 2), "dd/mm/yyyy") 'formats Cells containing a DATE in dd/mm/yyyy format
                            .Cells(iRow, 2).HorizontalAlignment = xlLeft 'formats Cell containing a DATE
Tried both the lines. No success so far L

I want the date to be stored always in the dd/mm/yyyy format

thx
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try

Code:
With .Cells(iRow, 2)
    .Value = DateValue(Me.DateBox.Value)
    .NumberFormat = "dd/mm/yyyy"
End With
 
Upvote 0
thank you VoG,
I changed a bit your code to accommodate it to mine
Code:
With ws
                        .Cells(iRow, 2).Value = DateValue(Me.DateBox.Value)
                            .Cells(iRow, 2).NumberFormat = "dd/mm/yyyy"
                            .Cells(iRow, 2).HorizontalAlignment = xlLeft
'some more code
end with

It seems to be working now..... or at least out of 10 tries I didn't manage to reproduce the previous behavior.


Any logical explanation on why the format didn't work?
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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