Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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中将返回的mystring值显示为日期_Vba_Excel - Fatal编程技术网

如何在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日