Excel 使用vba计算过期日期

Excel 使用vba计算过期日期,excel,vba,Excel,Vba,我试图写一个代码来计算到期日期自动使用vba。 我有一个包含员工名单的数据库,每个员工都有一份培训名单,上面有培训日期和到期日期。 我想通过点击按钮为每个人添加新的培训,添加培训名称和日期,并且必须自动计算到期日期(知道每个培训有一年的有效期) 到目前为止,我已经编写了以下代码: Public repertoire As String Private Sub CommandButton1_Click() repertoire = Application.GetOpenFile

我试图写一个代码来计算到期日期自动使用vba。 我有一个包含员工名单的数据库,每个员工都有一份培训名单,上面有培训日期和到期日期。 我想通过点击按钮为每个人添加新的培训,添加培训名称和日期,并且必须自动计算到期日期(知道每个培训有一年的有效期)

到目前为止,我已经编写了以下代码:

    Public repertoire As String

Private Sub CommandButton1_Click()

    repertoire = Application.GetOpenFilename()
    
   

Set ws = ActiveWorkbook.Worksheets(Personne)
Form_Intern =uF_Profil_Edit1.ListBox_Form_Intern.List(UF_Profil_Edit1.ListBox_Form_Intern.ListIndex, 0)
Date_Form_Intern = UF_Profil_Edit1.ListBox_Form_Intern.List(UF_Profil_Edit1.ListBox_Form_Intern.ListIndex, 1)
fin_col_Form_Intern = ws.Cells(10, 256).End(xlToLeft).Column

ws.Cells(10, fin_col_Form_Intern + 1) = Form_Intern ' training name 
ws.Cells(11, fin_col_Form_Intern + 1) = CDate(Date_Form_Intern) ' date
ws.Cells(15, fin_col_Form_Intern + 1) = CDate(Date_Form_Intern + 1095) ' expiration date
ws.Cells(12, fin_col_Form_Intern + 1) = repertoire
Me.Hide
Unload Me
   
End Sub
我代码中的问题就在这条线上

 ws.Cells(15, fin_col_Form_Intern + 1) = CDate(Date_Form_Intern + 1095) ' expiration date
它说类型不匹配


谁能告诉我哪里出错了,谢谢。

你的
日期\u表单\u实习生
看起来像一个字符串。不能向字符串中添加长字符串

要避免此错误,必须在以下位置进行更改:

ws.Cells(15, fin_col_Form_Intern + 1) = CDate(Date_Form_Intern) + 1095
但为什么要加1095呢

如果您必须将年份添加到
Date\u Form\u Intern
(作为日期),然后测试下一个函数,请:

Function ExpirationDate(Date_Form_Intern As Date) As Date
   ExpirationDate = DateSerial(Year(Date_Form_Intern) + 1, _
             Month(Date_Form_Intern), Day(Date_Form_Intern))
End Function
可以在代码中调用该函数,如下所示:

ws.Cells(15, fin_col_Form_Intern + 1) = ExpirationDate(CDate(Date_Form_Intern))

您是否需要在
到期日期
中添加一年?1095应该是什么?非常感谢我使用了你的功能,它工作得非常快速easy@FaruSZ:很高兴我能帮忙!但是,当有人花一些时间为我们提供问题的解决方案时,我们会在这里投票,更重要的是,勾选左侧代码侧复选框,以使其成为可接受的答案。在这种情况下,搜索类似问题的其他人将知道答案是有效的。。。