如何从Excel中的单元格获取日期 [英] How to get date from cell in excel

查看:87
本文介绍了如何从Excel中的单元格获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有VBA中的功能,该功能可处理单元格中的日期.但是当我选择单元格时出现了 By ref error .可能是什么问题?这是我的代码

I've got function in VBA that operates with dates from cells. But I got By ref error when I choose cells. What can be the problem? This is my code

Function DifferenceInYears(existdate As Date, estimdate As Date) As Double

    Dim yearDifference As Integer
    Dim monthDifference As Integer
    Dim dayDifference As Integer
    yearDifference = 0
    monthDifference = 0
    dayDifference = 0
    If (estimdate <= existdate) Then
        MsgBox "Input correct range"
        GoTo myerr
    End If
    Dim tempDate As Date
    IsDateLeapDay = False
    Dim existYear As String
    Dim estimYear As String
    existYear = Year(existdate)
    estimYear = Year(estimdate)
    estimMonth = Month(estimdate)
    existMonth = Month(existdate)

    and so on...

推荐答案

这应该使您入门:

Sub GetDatesAndComputeElapsedYears()

   Dim d1 As String
   Dim d2 As String

   d1 = Range("a2").Value2 'put a date in A2 Formatted as date(cell format)
   d2 = Range("b2").Value2 'put a date in B2 Formaated as date(cell format)

   Dim date1 As Date
   Dim date2 As Date

   date1 = CDate(d1) 'converts serialized date to DATE Object
   date2 = CDate(d2)

   Dim years

   years = DateDiff("yyyy", date1, date2) 'use this for date difference calculations

   MsgBox CStr(years), vbOKOnly, "Years Elapsed"

End Sub

这篇关于如何从Excel中的单元格获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆