用户定义的函数返回不可格式化的dateExcel VBA
我在Excel VBA中创build了一个UDF,它返回一个多维的date和双精度数组。 问题是我无法格式化返回的date。
这是一个简单的例子:
Function test(dates as Range) Dim results Dim i As Integer ReDim results(1 To dates.Cells.Count) For i = 1 To dates.Cells.Count results(i) = dates.Cells(i).Value Next test = Application.WorksheetFunction.Transpose(results) End Function
最后的转置只是为了方便有一个列输出(我按Ctrl + Shift + Enter)。 我使用这个简化的例子,你将无法格式化输出,它不会被视为严格意义上的date。
有任何想法吗?
将结果数组更改为双精度:
Function test(dates As Range) Dim results() As Double Dim i As Integer ReDim results(1 To dates.Cells.Count) For i = 1 To dates.Cells.Count results(i) = dates.Cells(i).Value Next test = Application.WorksheetFunction.Transpose(results) End Function
或者改变dates.Cells(i).Value
to dates.Cells(i).Value2
将返回double而不是datestring:
Function test(dates As Range) Dim results Dim i As Integer ReDim results(1 To dates.Cells.Count) For i = 1 To dates.Cells.Count results(i) = dates.Cells(i).Value2 Next test = Application.WorksheetFunction.Transpose(results) End Function
然后根据需要格式化单元格。
你可以尝试这样的事情。
Results(i) = CDate(dates.Cells(i).Value)