Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

excel - VBA - copying dates from one cell to another - strange change

So, it is rather strange but this is what is happening. In my code I have the following line:

tbl_Input.Cells(46 + (l_position - 1) * L_SIZE_BA, 12) = tbl_Input.Cells(46 + (l_position - 2) * L_SIZE_BA, 12)

which is supposed to simply take the value of tbl_Input.Cells(a,b) and put it in tbl_Input.Cells(c,d). However, the value of Cells(a,b) is "06.01.2016" and when I run the code I get "07.01.2020".

If I take the text property like this, I get what I want correctly:

tbl_Input.Cells(46 + (l_position - 1) * L_SIZE_BA, 12) = tbl_Input.Cells(46 + (l_position - 2) * L_SIZE_BA, 12).Text

My question is like: Why do I get the 07.01.2020 in first place, when I am not writing .Text or .Value?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

About a year later, just checking the questions in StackOverflow, the answer of this one is really obvious - I was probably having ThisWorkbook.Date1904 = True.

It gives exactly 4 years and a day difference, whenever you are copying something.

This is how to replicate it:

Public Sub TestMe()

    ThisWorkbook.Date1904 = True
    Cells(1, 1) = Date
    Cells(2, 1) = Cells(1, 1)
    ThisWorkbook.Date1904 = False

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...