如何在VBA中将返回的mystring值显示为日期
我创建了一个函数,根据考勤表中的缺勤条目获取缺勤天数 我的代码是:如何在VBA中将返回的mystring值显示为日期,vba,excel,Vba,Excel,我创建了一个函数,根据考勤表中的缺勤条目获取缺勤天数 我的代码是: 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) &
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”我假设缺席天数
是一个字符串
,您将得到2017年6月7日、2017年6月9日、2017年6月15日、2017年6月16日
作为字符串
(而不是日期
)
尝试下面的字符串
转换代码:
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
我假设AbsentDays
是一个字符串
,您将得到2017年6月7日、2017年6月9日、2017年6月15日、2017年6月16日
作为字符串
(而不是日期
)
尝试下面的字符串
转换代码:
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") & ", "
正因为我喜欢将更改保持在最低限度,我认为您可以简单地更改您的行:
myString = myString & Cells(myRow, cell.Column) & ", "
将是:
myString = myString & Format(CDate(Cells(myRow, cell.Column)), "dd mmm") & ", "
您现在获得的AbsentDays
的值和格式是什么?听起来您可能想使用myString=myString&format(Cells(myRow,cell.Column),“dd mmm”)&“,”
(如果该单元格包含日期)我得到这样的结果:2017年6月7日,2017年6月9日,2017年6月15日,2017年6月16日,但我想得到6月7日,6月9日……您现在获得的缺席天数的值和格式是什么?
?听起来您可能想使用myString=myString&format(Cells(myRow,cell.Column),“dd mmm”)&“,”
(如果该单元格包含日期)我得到的结果是:2017年6月7日,2017年6月9日,2017年6月15日,2017年6月16日,但我想得到6月7日,6月9日…我想在excel行中显示它。我是vba excel的新手。你能键入完整的代码吗?@YowE3K他得到的是日期数组吗?我以为这些就是他一个接一个的例子one@YowE3K给您:)我想在excel行中显示它。我是vba excel的新手。你能键入完整的代码吗?@YowE3K他得到的是日期数组吗?我以为这些就是他一个接一个的例子one@YowE3K给你,就给你:)哈哈,别忘了在end@ShaiRado现有的代码已经做到了这一点。(AbsentDays=Left(myString,Len(myString)-2)
)我可以向您发送excel文件吗。你能修改和帮助吗me@PratyushKumar如果我建议的更改不起作用,即使我有一份文件的副本,我也帮不了什么忙。(特别是我7分钟后就要睡觉了)。当您尝试更改我的代码时发生了什么?当您将Shai的代码添加到您的代码时会发生什么?(“不起作用”对诊断出什么地方出了问题没有多大帮助。)@PratyushKumar这可能是个愚蠢的问题,但6/15/2017
在贵国的日期格式中是否有效?(我正在努力思考格式(CDate(Cells(myRow,cell.Column)),“dd mmm”)
如何让值看起来像2017年6月15日
)哈哈,别忘了在end@ShaiRado现有的代码已经做到了这一点。(AbsentDays=Left(myString,Len(myString)-2)
)我可以向您发送excel文件吗。你能修改和帮助吗me@PratyushKumar如果我建议的更改不起作用,即使我有一份文件的副本,我也帮不了什么忙。(特别是我7分钟后就要睡觉了)。当您尝试更改我的代码时发生了什么?当您将Shai的代码添加到您的代码时会发生什么?(“不起作用”对诊断出什么地方出了问题没有多大帮助。)@PratyushKumar这可能是个愚蠢的问题,但6/15/2017
在贵国的日期格式中是否有效?(我正在努力思考格式(CDate(Cells(myRow,cell.Column)),“dd mmm”)
如何让值看起来像2017年6月15日
)