Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Vba将天转换为年、月、日_Excel_Vba - Fatal编程技术网

Excel Vba将天转换为年、月、日

Excel Vba将天转换为年、月、日,excel,vba,Excel,Vba,我有一段代码,显示了一个文件有多旧(按天计算): Date_Created = Worksheets("FileCleaner").Range("D" & startrow).Value File_Age_Day = DateDiff("d", Date_Created, Date) Worksheets("FileCleaner").Range("E" & startrow).Value = File_Age_Day 有没有办法把它转换成这样的格式,比如说“0年3个月3天”?

我有一段代码,显示了一个文件有多旧(按天计算):

Date_Created = Worksheets("FileCleaner").Range("D" & startrow).Value
File_Age_Day = DateDiff("d", Date_Created, Date)
Worksheets("FileCleaner").Range("E" & startrow).Value = File_Age_Day
有没有办法把它转换成这样的格式,比如说“0年3个月3天”?

试试下面的方法

date_created = Worksheets("FileCleaner").Range("D" & startrow).Value
File_Age_Day = DatePart("yyyy", date_created) & " years, " & DatePart("M", 
date_created) & " Months, " & DatePart("d", date_created) & " days"
Worksheets("FileCleaner").Range("E" & startrow).Value = File_Age_Day

正如其他用户已经注意到的,您将遇到困难,因为不同的月份有不同的天数和周数(给定的年份也可能有不同的天数)

我认为你最好的选择是“假设”一个月有一定的天数(例如30天),而你不在闰年。那么你的算法应该是这样的:

Private Const DAYS_IN_YEAR As Integer = 365
Private Const DAYS_IN_MONTH As Integer = 30

Public Sub OutputElapsed(days As Integer)
    Dim years As Integer, months As Integer, daysRemaining As Integer

    years = Int(days / DAYS_IN_YEAR)
    daysRemaining = days - (years * DAYS_IN_YEAR)

    months = Int(daysRemaining / DAYS_IN_MONTH)
    daysRemaining = daysRemaining - (months * DAYS_IN_MONTH)

    Debug.Print _
            years & " years, " & _
            months & " months, " & _
            daysRemaining & " days"
End Sub

这是我为完成这项任务而设计的潜艇。我从你的代码中退了一步。如果我没有看错你的问题,你有一个单元格,其中包含一个日期
范围(“D”&startrow)
,然后你计算自该日期起的原始天数,然后将该数字转换为年、月、日。我的代码取而代之的是单元格中的日期字符串(例如:2019年3月1日),然后分别计算过去的年、月和日。它考虑了每个月不同的天数和闰年,通过我的测试,唯一没有处理的逻辑是,如果单元格中的日期实际上是在未来-我希望这没问题

Sub DateCalc()
    'Worksheet Variables
    Dim mySheet As Worksheet
    Dim dateCell, outputCell As Range

    Set mySheet = Sheets("Sheet1")
    Set dateCell = mySheet.Cells(1, 1)
    Set outputCell = mySheet.Cells(1, 2)

    'Date variables
    Dim fileDate, curDate, tempDate As Date
    Dim fileYear, curYear, tempYear, years, _
    fileMonth, curMonth, tempMonth, months, _
    fileDay, curDay, tempDay, days, _
    curDPM, fileDPM _
    As Integer

    'Get date of file and calculate year, month, and day
    fileDate = dateCell.Value
    fileYear = CInt(Split(fileDate, "/")(2))
    fileMonth = CInt(Split(fileDate, "/")(0))
    fileDay = CInt(Split(fileDate, "/")(1))

    'Get the current date, year, month, and day
    curDate = Date
    curYear = CInt(Split(curDate, "/")(2))
    curMonth = CInt(Split(curDate, "/")(0))
    curDay = CInt(Split(curDate, "/")(1))

    'Calculate years passed
    If curYear > fileYear Then
        years = curYear - fileYear
    End If
    If years = "" Then
        years = 0
    End If

    'Calculate months passed
    If curMonth > fileMonth Then
        months = curMonth - fileMonth
    ElseIf curMonth = fileMonth Then
        months = 0
    ElseIf curMonth < fileMonth Then
        months = (12 - fileMonth) + curMonth
    End If

    'Calculates days per month (DPM) for current year
    Select Case curMonth
        Case 4 Or 6 Or 9 Or 11
            '31-Day months
            'April, June, September, November.
            curDPM = 30
        Case 2
            'February will either have 29 or 28 days
            'If the current year is divisible by 4 it
            'is a leap year and there are 29
            curDPM = IIf(curYear Mod 4 = 0, 29, 28)
        Case Else
            '31-Day months
            curDPM = 31
    End Select

    'Calculates days per month (DPM) for file year
    Select Case fileMonth
        Case 4 Or 6 Or 9 Or 11
            fileDPM = 30
        Case 2
            fileDPM = IIf(fileYear Mod 4 = 0, 29, 28)
        Case Else
            fileDPM = 31
    End Select

    'Calculates days passed
    If curDay > fileDay Then
        days = curDay - fileDay
    ElseIf (curDay = fileDay) Then
        days = 0
    ElseIf (curDay < fileDay) Then
        days = (fileDPM - fileDay) + curDay
    End If

    'years, months, and days are calculate independently
    'so this loop corrects them to work together
    'Ex: 12/31/2000 to 1/1/2001 would be 1 year, 1 month, 1 day without this loop
    Do
        tempDate = DateAdd("yyyy", years, fileDate)
        tempDate = DateAdd("m", months, tempDate)
        tempDate = DateAdd("d", days, tempDate)

        tempYear = CInt(Split(tempDate, "/")(2))
        tempMonth = CInt(Split(tempDate, "/")(0))
        tempDay = CInt(Split(tempDate, "/")(1))

        If tempYear > curYear Then
            years = years - 1
        ElseIf tempYear < curYear Then
            years = years + 1
        End If

        If tempMonth > curMonth Then
            months = months - 1
        ElseIf tempMonth < tempMonth Then
            months = months + 1
        End If

        If tempDay > curDay Then
            days = days - 1
        ElseIf tempDay < curDay Then
            days = days + 1
        End If
    Loop While tempDate <> curDate

    'Set cell to display time passed
    outputCell.Value = years & " Years, " & months & " Months, " & days & " Days"
End Sub
Sub-DateCalc()
'工作表变量
将我的工作表设置为工作表
Dim dateCell,outputCell作为范围
设置mySheet=Sheets(“Sheet1”)
Set dateCell=mySheet.Cells(1,1)
Set outputCell=mySheet.Cells(1,2)
'日期变量
Dim fileDate、curDate、tempDate As Date
一年,一年,一年,一年_
fileMonth、curMonth、tempMonth、months、_
fileDay、curDay、tempDay、days、_
curDPM,fileDPM_
作为整数
'获取文件日期并计算年、月和日
fileDate=dateCell.Value
fileYear=CInt(拆分(文件日期“/”)(2))
fileMonth=CInt(拆分(fileDate,“/”)(0))
fileDay=CInt(拆分(fileDate,“/”)(1))
'获取当前日期、年、月和日
curDate=日期
curYear=CInt(拆分(curDate,“/”(2))
curMonth=CInt(拆分(curDate,“/”)(0))
curDay=CInt(拆分(curDate,“/”(1))
"数年过去了,
如果curYear>fileYear,则
年份=curYear-fileYear
如果结束
如果年份=“那么
年=0
如果结束
"数月过去了,
如果curMonth>fileMonth,则
月份=curMonth-fileMonth
ElseIf curMonth=fileMonth然后
月份=0
ElseIf curMonthfileDay,则
days=curDay-fileDay
ElseIf(curDay=fileDay)则
天数=0
ElseIf(curDaycurYear,则
年=年-1
如果是临时年<现在年
年=年+1
如果结束
如果tempMonth>curMonth,则
月=月-1
如果临时月<则临时月
月数=月数+1
如果结束
如果tempDay>curDay,则
天=天-1
如果是这样的话
天数=天数+1
如果结束
在tempDate curDate时循环
'将单元格设置为显示经过的时间
outputCell.Value=年和年、月和月、日和日
端接头

要使其适用于单独的工作表,您只需更改mySheet、dateCell和outputCell变量。“我的日期”单元格的格式设置为mm/dd/yyyy。我没有用其他日期格式测试过它。

这是我的解决方案。我在我的公司里用这个,虽然它是用印尼语写的,因为我来自印尼。这个函数包含两个参数:tanggal_-akhir(翻译为“开始日期”)和tanggal_-mulai(翻译为“结束日期”)。公式是BEDATANGGAL,意思是“日期差”。希望能有帮助

Function BEDATANGGAL(tanggal_mulai As Date, tanggal_akhir As Date)

Dim hari As Integer
Dim bulan As Integer
Dim tahun As Integer
Dim durasi As Integer
Dim tambah As Date

hari = 0
bulan = 0
tahun = 0
tambah = tanggal_mulai

durasi = DateDiff("d", tanggal_mulai, tanggal_akhir) - 1

For i = 0 To durasi

hari = hari + 1
tambah = tambah + 1

If Day(tanggal_mulai) = Day(tambah) Then

    hari = 0
    bulan = bulan + 1

    End If

If bulan = 12 Then

    hari = 0
    bulan = 0
    tahun = tahun + 1

    End If

Next i

BEDATANGGAL = tahun & " tahun, " & bulan & " bulan, " & hari & " hari"

End Function

泰勒对上述答案的修改

Function date_duration(start_date, end_date)
'Worksheet Variables

Dim dateCell, outputCell As Range



'Date variables
Dim fileDate, curDate, tempDate As Date
Dim fileYear, curYear, tempYear, years, _
fileMonth, curMonth, tempMonth, months, _
fileDay, curDay, tempDay, days, _
curDPM, fileDPM _
As Integer

'Get date of file and calculate year, month, and day
fileDate = start_date
fileYear = CInt(Split(fileDate, "/")(2))
fileMonth = CInt(Split(fileDate, "/")(0))
fileDay = CInt(Split(fileDate, "/")(1))

'Get the current date, year, month, and day
curDate = end_date
curYear = CInt(Split(curDate, "/")(2))
curMonth = CInt(Split(curDate, "/")(0))
curDay = CInt(Split(curDate, "/")(1))

'Calculate years passed
If curYear > fileYear Then
    years = curYear - fileYear
End If
If years = "" Then
    years = 0
End If

'Calculate months passed
If curMonth > fileMonth Then
    months = curMonth - fileMonth
ElseIf curMonth = fileMonth Then
    months = 0
ElseIf curMonth < fileMonth Then
    months = (12 - fileMonth) + curMonth
End If

'Calculates days per month (DPM) for current year
Select Case curMonth
    Case 4 Or 6 Or 9 Or 11
        '31-Day months
        'April, June, September, November.
        curDPM = 30
    Case 2
        'February will either have 29 or 28 days
        'If the current year is divisible by 4 it
        'is a leap year and there are 29
        curDPM = IIf(curYear Mod 4 = 0, 29, 28)
    Case Else
        '31-Day months
        curDPM = 31
End Select

'Calculates days per month (DPM) for file year
Select Case fileMonth
    Case 4 Or 6 Or 9 Or 11
        fileDPM = 30
    Case 2
        fileDPM = IIf(fileYear Mod 4 = 0, 29, 28)
    Case Else
        fileDPM = 31
End Select

'Calculates days passed
If curDay > fileDay Then
    days = curDay - fileDay
ElseIf (curDay = fileDay) Then
    days = 0
ElseIf (curDay < fileDay) Then
    days = (fileDPM - fileDay) + curDay
End If

'years, months, and days are calculate independently
'so this loop corrects them to work together
'Ex: 12/31/2000 to 1/1/2001 would be 1 year, 1 month, 1 day without this loop
Do
    tempDate = DateAdd("yyyy", years, fileDate)
    tempDate = DateAdd("m", months, tempDate)
    tempDate = DateAdd("d", days, tempDate)

    tempYear = CInt(Split(tempDate, "/")(2))
    tempMonth = CInt(Split(tempDate, "/")(0))
    tempDay = CInt(Split(tempDate, "/")(1))

    If tempYear > curYear Then
        years = years - 1
    ElseIf tempYear < curYear Then
        years = years + 1
    End If

    If tempMonth > curMonth Then
        months = months - 1
    ElseIf tempMonth < tempMonth Then
        months = months + 1
    End If

    If tempDay > curDay Then
        days = days - 1
    ElseIf tempDay < curDay Then
        days = days + 1
    End If
Loop While tempDate <> curDate

'Set cell to display time passed
date_duration = years & " Years, " & months & " Months, " & days & " Days"
End Function
功能日期\持续时间(开始日期、结束日期)
'工作表变量
Dim dateCell,outputCell作为范围
'日期变量
Dim fileDate、curDate、tempDate As Date
一年,一年,一年,一年_
fileMonth、curMonth、tempMonth、months、_
fileDay、curDay、tempDay、days、_
curDPM,fileDPM_
作为整数
'获取文件日期并计算年、月和日
fileDate=开始日期
fileYear=CInt(拆分(文件日期“/”)(2))
fileMonth=CInt(拆分(fileDate,“/”)(0))
fileDay=CInt(拆分(fileDate,“/”)(1))
'获取当前日期、年、月和日
curDate=结束日期
curYear=CInt(拆分(curDate,“/”(2))
curMonth=CInt(拆分(curDate,“/”)(0))
curDay=CIn