简体   繁体   中英

Excel VBA - Saving file changes date format

I am trying to automate a process we have to be as smooth as possible by using a macro in Excel to drag formulae down as far as my master data needs, then saving each sheet as a prn file.

The issue is that where there are dates, it is saving the format as American dates so when we upload the data into our system it is either wrong or causes an error (eg today I tried to upload 30/04/2015 but my system said there is no such date.

When I use MONTH() on the cell before AND after running the macro it gives the correct month, but the prn files it has saved down have the date incorrectly stored (the above appears as 4/30/2015 whereas my Excel sheet still says 30/4/2015 .

I've tried storing the date in US format but it then just retains the format as US format

Below is a MWE of what I have written so far, if anyone could help with how to ensure that dates are stored in the UK format this would be much appreciated.

Sub Uploads()

Dim wb As Workbook, wk As Worksheet, rng As Range, num As Long, i As Long, str As String
Set wb = ThisWorkbook
i = 0
num = wb.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row - 7

For Each wk In wb.Worksheets
    wk.Range("A2:Z2").AutoFill Destination:=wk.Range("A2:Z" & num), Type:=xlFillDefault
    wk.Columns.AutoFit
    wk.SaveAs Filename:="H:\Finance\Uploads\CBCS\Upload Master\CBCS (" & i & ") " & Format(Now, "dd-mm-yyyy") & ".prn", FileFormat:=xlTextPrinter
    i = i + 1
Next wk

End Sub

如果您在导出之前手动设置单元格的数字格式,则它应该可以工作。

wk.Range("A2:Z" & num).NumberFormat = "d/M/yyyy" 

Try to adjust your system settings first... I had experience in the past that if different PCs have different settings it could affect how they see the dates and how macro reacts...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM