Export sheet date format not correct

dappy

Board Regular
Joined
Apr 23, 2018
Messages
124
Office Version
  1. 2013
Platform
  1. Windows
Morning folks,

So, i have a sheet that is exported from a workbook using workbook adds, the date format in the workbook is yyyy-mm-dd but the format in the sheet after export is lost and reverts back to general format. is it possible to copy a sheet to a new sheet and keep the format?

thanks in advance

Carl
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Please explain in more detail exactly how you are doing the export, and then how you are opening the new file.
 
Upvote 0
thanks, I have this to export

VBA Code:
 Set NewBook = Workbooks.Add
  Workbooks("LTE_batch_selection_3.xlsm").Worksheets("batch_output").Range("A1:ab1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
 ' Worksheets("Sheet1").Name = "Batch_" & myValue
    Dim bFileSaveAs As Boolean
    bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show

the new file opens automatically from here.
 
Upvote 0
I think maybe by using ".PasteSpecial (xlPasteValues)", you are copying only the values and not any of the formats.
What happens if you just use ".Paste" instead, which should brining the formatting allong with it?

If your intention is to turn the formulas into hard-coded values, try do a Copy -> Paste Special Values over top of itself AFTER doing the initial Copy/Paste.
 
Upvote 0
mmm i tried the Paste only and had no output, sheet was empty.

Can you explain a bit more for the Copy -> paste special values?
 
Upvote 0
You can use the Macro Recorder to get most of the code you need.
I did that, and cleaned up the code to look something like this:
VBA Code:
Sub MyCopy()

 Dim NewBook As Workbook

 Set NewBook = Workbooks.Add

    Workbooks("Book2.xlsm").Worksheets("batch_output").Range("A1:AB1000").Copy NewBook.Worksheets("Sheet1").Range("A1")
  
    Range("A1:AB1000").Copy
    Range("A1:AB1000").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AB").EntireColumn.AutoFit
  
 ' Worksheets("Sheet1").Name = "Batch_" & myValue
    Dim bFileSaveAs As Boolean
    bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
    
End Sub
 
Upvote 0
Also xlPasteValuesAndNumberFormats can be used instead of xlPasteValues
VBA Code:
  Set NewBook = Workbooks.Add
  Workbooks("LTE_batch_selection_3.xlsm").Worksheets("batch_output").Range("A1:ab1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
 
Upvote 0
Solution
Also xlPasteValuesAndNumberFormats can be used instead of xlPasteValues
Even better than my solution!
I was not aware that option existed!
 
Upvote 0
folks, that's great, they both work well thank you but..

is it possible to get the format yyyy-mm-dd instead of dd-mm-yyyy?
 
Upvote 0
is it possible to get the format yyyy-mm-dd instead of dd-mm-yyyy?
Since you are using VBA code, it would be pretty easy to add a line of code to format the columns any way you want.
Just turn on the Macro Recorder and record yourself applying your desired format to the columns you want, and you will have the code you need.
Just copy and paste that code into your current VBA code.
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,338
Members
449,155
Latest member
ravioli44

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