Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
Vba 将间隔格式化为“YMD”格式,格式为函数中的一个参数_Vba_Function_Date - Fatal编程技术网

Vba 将间隔格式化为“YMD”格式,格式为函数中的一个参数

Vba 将间隔格式化为“YMD”格式,格式为函数中的一个参数,vba,function,date,Vba,Function,Date,以下是我的代码: Option Explicit Public Function StudyDuration(ByVal Start As Date, Format As String, Optional ByVal Graduation As Date, Optional ByVal Expected As Date) As String Dim TillGraduation As Integer StudyDuration = Graduation Or Expected - S

以下是我的代码:

Option Explicit
Public Function StudyDuration(ByVal Start As Date, Format As String, Optional ByVal Graduation As Date, Optional ByVal Expected As Date) As String

  Dim TillGraduation As Integer

  StudyDuration = Graduation Or Expected - Start
  TillGraduation = DateDiff("ymd", Date, Expected)

  If Graduation = 0 And Expected = 0 Then Graduation = Date

  Range("B1").Value = Start
  Range("B2").Value = Graduation
  Range("B3").Value = Expected
  Range("B4").Value = StudyDuration
  Range("B5").Value = TillGraduation


  If Graduation = 0 And Expected > Start Then

  StudyDuration = Expected - Start And TillGraduation =     DateDiff("ymd", Date, Expected)

  MsgBox ("Study Length" & " is" & Range("B4").Value & vbNewLine & Range("B5") & "till Graduate")

  End If
End Function
但是,单元格B1始终显示日期1900年1月7日。谁能告诉我该怎么纠正它吗


非常感谢

我认为函数不允许修改单元格值。函数只应返回一个值,该值将设置从中调用的单元格的值

如果要修改单元格值,则应改为使用子单元格


顺便说一句,您的函数没有使用Format参数

这将有助于准确显示调用此函数的方式。不要在函数UpsetCompiler后命名参数这一行看起来很奇怪StudyDuration=Expected-Start和TillDigration=DateDiffymd,Date,ExpectedFunctions应该执行计算并返回结果,除非范围是参数之一,否则不要更改范围值。另请参阅。