将工作表复制到新工作簿时日期发生奇怪变化。

huangapple go评论184阅读模式
英文:

Weird Date change when copying a Worksheet into a new workbook

问题

当我使用以下代码创建一个带有复制工作表的新工作簿时,会出现奇怪的日期变化:

Worksheets("周模板").Copy
Set Newwb = ActiveWorkbook
NewwbName = ActiveWorkbook.Name

Windows("主工作簿.xlsm").Activate
Sheets("周模板").Select
Range("B3:B13").Select
Application.CutCopyMode = False
Selection.Copy

Workbooks(NewwbName).Worksheets("周模板").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

例如,如果我复制了日期 "1/1/2023",它会变成 "12/31/2018"。

然而,当我将单元格格式更改为文本时,数字保持相同,为 "43465"。

有谁知道如何解决这个问题吗?

英文:

When I use the following code to create a new workbook with a copied sheet there is a strange date change that occurs:

Worksheets("Week Template").Copy
Set Newwb = ActiveWorkbook
NewwbName = ActiveWorkbook.Name

Windows("Mainworkbook.xlsm").Activate
Sheets("Week Template").Select
    Range("B3:B13").Select
    Application.CutCopyMode = False
    Selection.Copy

Workbooks(NewwbName).Worksheets("Week Template").Activate
Range("B3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

For example, if I copied the date "1/1/2023" it is changed to "12/31/2018"

however, when I change the format of the cells to text, the numbers are the same, "43465"

Does anyone know how I can resolve this issue?

答案1

得分: 5

差异是因为您的工作簿之一(日期较晚的工作簿)已设置为使用1904日期系统。

在MAC上,默认情况下通常使用1904日期系统,而在Windows上使用1900日期系统。

您可以在“文件/Excel选项/高级/在计算此工作簿时”下进行设置:

将工作表复制到新工作簿时日期发生奇怪变化。

您可以使用Workbook.Date1904属性来确定工作簿的设置,并根据需要进行适当的调整,以保持日期相同。

英文:

The difference is due to the fact that one of your workbooks (the one with the later date) is set to use the 1904 Date system.

1904 date system was generally used by default on the MAC, and 1900 date system in Windows.

This can be set under File/Excel Options/Advanced/When calculating this workbook:

将工作表复制到新工作簿时日期发生奇怪变化。

You can use the Workbook.Date1904 property to determine the workbook setting, and make the appropriate adjustment in the value to keep the dates the same.

答案2

得分: 1

以下是翻译好的部分:

"Following on from my comment above. Try like this:

Dim wb As Workbook, wbNew As Workbook, wsTempl As Worksheet

Set wb = ThisWorkbook 'for example, if it's the workbook where this code runs
Set wsTempl = wb.Worksheets("Week Template")

wsTempl.Copy
Set wbNew = ActiveWorkbook 'has the copied sheet
wbNew.Worksheets(wsTempl.Name).Range("B3:B13").Value = wsTempl.Range("B3:B13").Value

If you still see the problem then there's something going on we don't have enough information to figure out. How many dates are affected, what are the date formats applied in the source worksheet, etc."

英文:

Following on from my comment above. Try like this:

    Dim wb As Workbook, wbNew As Workbook, wsTempl As Worksheet
    
    Set wb = ThisWorkbook 'for example, if it's the workbook where this code runs
    Set wsTempl = wb.Worksheets("Week Template")
    
    wsTempl.Copy
    Set wbNew = ActiveWorkbook 'has the copied sheet
    wbNew.Worksheets(wsTempl.Name).Range("B3:B13").Value = wsTempl.Range("B3:B13").Value

If you still see the problem then there's something going on we don't have enough information to figure out. How many dates are affected, what are the date formats applied in the source worksheet, etc.

huangapple
  • 本文由 发表于 2023年1月9日 01:36:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定