如何在VBA中显示返回的mystring值作为date

我根据出席表中的缺席条目创build了缺席日的function。

我的代码是:

Dim cell As Range Dim myString As String 'Loop through all the cells in the range For Each cell In myRange 'Check for value If cell = "A" Then myString = myString & Cells(myRow, cell.Column) & ", " End If Next cell 'Return string If Len(myString) > 0 Then AbsentDays = Left(myString, Len(myString) - 2) End If End Function 

我想显示返回值显示为"dd mmm"

我假设AbsentDays是一个String ,并且您6/7/2017, 6/9/2017, 6/15/2017, 6/16/2017 String (而不是Date )获得6/7/2017, 6/9/2017, 6/15/2017, 6/16/2017

尝试下面的String转换代码:

 Dim AbsentDays As String Dim DatesArr As Variant Dim i As Long ' for my tests AbsentDays = "6/7/2017, 6/9/2017, 6/15/2017, 6/16/2017" ' use the split to get the values in an array DatesArr = Split(AbsentDays, ",") Dim myDateformat As Variant ' loop through array For i = 0 To UBound(DatesArr) If i = 0 Then ' first array element myDateformat = Format(CDate(DatesArr(i)), "dd mmm") Else ' 2nd array element and above myDateformat = myDateformat & ", " & Format(CDate(DatesArr(i)), "dd mmm") End If Next i MsgBox myDateformat 

只是因为我喜欢把改变降到最低,我想你可以简单地改变你的评论:

 myString = myString & Cells(myRow, cell.Column) & ", " 

成为:

 myString = myString & Format(CDate(Cells(myRow, cell.Column)), "dd mmm") & ", "