在代码中,日期是100%正确的。当它变成表格形式时,显示为“ 1/0/1900”。
在VBA中使用公式时,这似乎是一个问题。
function person_2nd(name, surname, age, favourite_colour) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_colour = favourite_colour;
this.new_favourite_colour = function(favourite_colour) {
this.favourite_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
答案 0 :(得分:3)
引号通常是公式中的难题。我不知道您要达到什么目标,但是这些版本可以工作:
ActiveCell.Offset(1).FormulaR1C1 = "=""" & TestDate & """" 'output: ="10/1/2018"
ActiveCell.Offset(2).Formula = "=""" & TestDate & """" 'output: ="10/1/2018"
ActiveCell.Offset(3).FormulaR1C1 = TestDate 'output: 10/1/2018
ActiveCell.Offset(4).Formula = TestDate 'output: 10/1/2018
答案 1 :(得分:1)
当您混合使用日期和字符串时,就会发生这种情况。TestDate
不是存储为日期,而是存储为字符串9/1/2018
。当您将此字符串粘贴到第三行ActiveCell.Offset(1).FormulaR1C1 = "=" & TestDate & "" 'output: 1/0/1900
的单元格中时,输出为=9/1/2018
(0.00445986124876115)。在内部,第0天为1/1/1900。您必须将TestDate
字符串转换为日期。
答案 2 :(得分:0)
啊-明白了。。。让它起作用的唯一方法是执行以下操作。希望这对您有用!
Public Sub Test()
Dim formula As String
ThisWorkbook.ActiveSheet.Cells(1, 1).Select
TestDate = DateAdd("m", 1, "9/1/2018")
ActiveCell = TestDate
formula = "=Date(" + CStr(Year(TestDate)) + "," + CStr(Month(TestDate)) + "," + CStr(Day(TestDate)) + ")"
ActiveCell.Offset(1).FormulaR1C1 = formula
ActiveCell.Offset(2).formula = formula
End Sub