如何在VBA中从单元格中获取当前日期和给定日期之间的差异?

时间:2016-10-17 11:31:43

标签: excel vba excel-vba

我是VBA的新手。我想从用户那里得到当前日期和给定日期之间的差异。我怎么解决这个问题? enter image description here当我点击计算点按钮时,它会显示#NAME?

这是我的代码:

Private Sub Sum_Click()
Dim LastRow As Long

LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range("F6:F" & LastRow).Formula = "=DateDiff(""y"", Now, c6)"
End Sub

2 个答案:

答案 0 :(得分:0)

Range("F6:F" & LastRow).Formula = "=Text(Now()-C6, "y")"应该这样做。

有关其他格式选项,请参阅text function

特别是:

Image from the supplied link to support.office.com

答案 1 :(得分:0)

由于您需要多年的差异,您可以使用DateDif

scala.collection.immutable.Map<String, String> immutableMap = new scala.collection.immutable.HashMap<String,String>().empty();
for (Map.Entry<String, String> entry : session.entrySet()) {
  immutableMap = immutableMap.$plus(new Tuple2<>(entry.getKey(), entry.getValue()));
}
play.api.mvc.Session newSession = new play.api.mvc.Session(immutableMap);