VBA Changes Date Format

FlashNZ

New Member
Joined
Mar 3, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some VBA that allows me to edit data via a Userform, it then saves the changes to a range in a worksheet. The trouble is that when I save the changes to the date field it changes the date format from dd/mm/yyy to mm/dd/yyyy, e.g from 02/11/2020 to 11/02/2020. The Value that contains the Date is Emp2.

How do I fix this?

VBA Code:
Private Sub cmdEdit_Click()
'declare the variables
Dim findvalue As Range
Dim cNum As Integer
Dim DataSH As Worksheet
'error handling
On Error GoTo errHandler:
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
Set DataSH = Sheet1
'check for values
If Emp1.Value = "" Or Emp2.Value = "" Then
MsgBox "There is not data to edit"
Exit Sub
End If
'clear the listbox
lstEmployee.RowSource = ""
'find the row to edit
Set findvalue = DataSH.Range("B:B"). _
Find(What:=Me.Emp1.Value, LookIn:=xlValues, LookAt:=xlWhole)
'update the values
findvalue = Emp1.Value
findvalue.Offset(0, 1) = Emp2.Value
findvalue.Offset(0, 2) = Emp3.Value
findvalue.Offset(0, 3) = Emp4.Value
findvalue.Offset(0, 4) = Emp5.Value
findvalue.Offset(0, 5) = Emp6.Value
findvalue.Offset(0, 6) = Emp7.Value
findvalue.Offset(0, 7) = Emp8.Value
findvalue.Offset(0, 8) = Emp9.Value
findvalue.Offset(0, 9) = Emp10.Value
findvalue.Offset(0, 10) = Emp11.Value
'unprotect the worksheets for the advanced filter
'Unprotect_All
'filter the data
DataSH.Range("B8").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("Data!$Q$8:$Q$9"), CopyToRange:=Range("Data!$S$8:$AC$8"), _
Unique:=False
'if no data exists then clear the rowsource
If DataSH.Range("S9").Value = "" Then
lstEmployee.RowSource = ""
Else
'add the filtered data to the rowsource
lstEmployee.RowSource = DataSH.Range("outdata").Address(external:=True)
End If
'return to sheet
Sheet2.Select
'Protect all sheets
'Protect_All
'error block
On Error GoTo 0
Exit Sub
errHandler:
'Protect all sheets
'Protect_All
'show error information in a messagebox
MsgBox "An Error has Occurred " & vbCrLf & _
"The error number is: " & Err.Number & vbCrLf & _
Err.Description & vbCrLf & "Please notify the administrator"
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
As VBA knows only the US date format, if your Windows Regional date setting is not the same format​
so just use the cell property FormulaLocal when allocating the local date …​
Another way is to convert the text as a date via the VBA function CDate …​
 
Upvote 0
As VBA knows only the US date format, if your Windows Regional date setting is not the same format​
so just use the cell property FormulaLocal when allocating the local date …​
Another way is to convert the text as a date via the VBA function CDate …​
Thanks for the quick reply and I thought it might have something to do with regional settings, I'm in New Zealand so we don't use the US format.

Any idea how I fit that into my code?
 
Upvote 0
As both ways work, try CDate function for example …​
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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