Как отобразить возвращаемое значение mystring в качестве даты в VBA
Я создал функцию для отсутствия дней, основанных на отсутствии записей в моем листе посещаемости.
Мой код:
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
).
Попробуйте код преобразования 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") & ", "