Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 can';t将日期转换为字符串_Vba_Excel - Fatal编程技术网

VBA can';t将日期转换为字符串

VBA can';t将日期转换为字符串,vba,excel,Vba,Excel,我有一张excel表格,可以计算出牢房内当前的星期天 我使用宏来处理图纸中的数据。 但我无法读出日期并将其转换为字符串 运行时错误“13”:类型不匹配。 在线= Sub Dateit() Dim sDate As String 'Formula inside cell Q5: =Today() 'Formula inside merged cells Q6:R6: =INT((Q5-1)/7)*7+1 'Shows the String: 5-Nov-2017

我有一张excel表格,可以计算出牢房内当前的星期天

我使用宏来处理图纸中的数据。 但我无法读出日期并将其转换为字符串

运行时错误“13”:类型不匹配。 在线=

Sub Dateit()

    Dim sDate As String
    'Formula inside cell Q5: =Today()
    'Formula inside merged cells Q6:R6: =INT((Q5-1)/7)*7+1
    'Shows the String: 5-Nov-2017 in cells q6:r6

    sDate = Format(Range("Q6:R6").Value, "dd. mmm yyyy")
    MsgBox sDate

End Sub

如果您的示例中有多个值/日期,那么我建议您使用如下数组:

Option Explicit

Sub Dateit()

Dim sDate() As Variant
Dim x As Long, y As Long

'Load all dates into an array
sDate() = ActiveSheet.Range("Q6:R6").Value

'Iterate through all the "rows" of the array (dimension 1)
For y = LBound(sDate, 1) To UBound(sDate, 1)
    'Iterate through all the "columns" of the array (dimension 2)
    For x = LBound(sDate, 2) To UBound(sDate, 2)
        'refomrat the values as dates
        sDate(y, x) = Format(sDate(y, x), "dd. mmm yyyy")
        'show the dates in a message box
        MsgBox sDate(y, x)
    Next x
Next y

End Sub

如果您提到一个错误,如果不发布该错误的全文,并告诉读者IDE突出显示的是哪一行,这是没有用的。请尝试Range(“Q6”).Value而不是Range(“Q6:R6”).ValueAs@ExcelDevelopers说的那样-您正在向函数传递一个值范围,而函数只需要一个值。您应该只使用Range(“Q6”)对于value.as@ExcelDevelopers,在使用合并单元格时,需要使用第一列的引用。