无法将我的变量转换为 CDate 或 Serial Date,出现不匹配错误 [英] Cannot convert my variables to CDate or Serial Date, getting Mismatch error

查看:18
本文介绍了无法将我的变量转换为 CDate 或 Serial Date,出现不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里准备了许多关于不匹配错误的帖子,但我尝试的每种格式仍然出现相同的错误.

Dim calendarDate, sDate, sFinalcalendarDate = "Sun Apr 05 00:00:00 CDT 2020"sYear = Right(calendarDate, 4)sDay = Mid(calendarDate,9,2)sMonth = Mid(calendarDate,5,3)如果 sMonth = Apr"然后 sMonth = 04"否则 sMonth = sMonth万一sDate = sYear &——"&sMonth &——"&天sFinal = CDate(sDate)

我收到类型不匹配 CDate 错误,但格式应该可以工作?我也试过 MM/DD/YYYY.

而且我试过 sFinal = DateSerial(sYear,sMonth,SDay) 也不起作用.但是如果你不使用变量...

sFinal = DateSerial("2020","04","05") 这是有效的.我不明白为什么我的 SYear、SMonth 和 SDay 不起作用,因为它们是相同的数字."

我的最终目标是从日历日期中减去 14 天,但我什至无法将我的变量转换为日期格式来减去它......所以也许我应该做一些更简单的事情在这里做什么?

感谢大家提供的任何帮助,非常感谢!长期读者,第一次发帖.

解决方案

最简单的操作方法是使用 Split() 作为每个值用空格分隔(Chr(32)).

你最终得到的是一个数组,其中包含组成由空格字符分割的字符串的每个元素(Chr(32)),因此通过将值连接在一起,我们可以构建我们的日期-时间如何 CDate() 期望,不包括分别为 SunCDT 的第一个和第六个元素,如以及对传递给 CDate() 的构造字符串中的年份重新排序.

Dim input: input = "Sun Apr 05 00:00:00 CDT 2020";昏暗数据:数据 = 拆分(输入,Chr(32))'忽略数组中的第一个和第六个元素并构建我们的日期值Dim 输出:output = CDate(data(1) & " & data(2) & " & data(5)) &""&时间值(数据(3))调用 Wscript.Echo(output)

输出:

05/04/2020 00:00:00

<块引用>

注意:结果将基于用户的区域设置,因为 CDate() 在解析 Date 字符串时使用它.


有用的链接

I've ready numerous posts here about the mismatch error but every format I try I still get the same error.

Dim calendarDate, sDate, sFinal

calendarDate = "Sun Apr 05 00:00:00 CDT 2020"
sYear = Right(calendarDate, 4)
sDay = Mid(calendarDate,9,2)
sMonth = Mid(calendarDate,5,3)

If sMonth = "Apr" Then sMonth = "04" Else sMonth = sMonth
End if

sDate = sYear & "-" & sMonth & "-" & sDay


sFinal = CDate(sDate)

I get the type mismatch CDate error but the format should work? I have also tried MM/DD/YYYY.

And I have tried sFinal = DateSerial(sYear,sMonth,SDay) also does not work. But if you don't use the variables...

sFinal = DateSerial("2020","04","05") this works. I don't understand why my SYear, SMonth, SDay would not work as they are the same numbers."

My end goal here is to subtract 14 days from the calendar date but i can't even get my variable into a date format to subtract it...so maybe there is something simpler I should be doing here?

Thank you all for any help you can give much appreciated! Long time reader, first time posting.

解决方案

The simplest way to manipulate will be to use Split() as each value separated by a Space (Chr(32)).

What you end up with is an array containing each element that made up the string split by the Space character (Chr(32)), so by concatenating values back together we can construct our date-time how CDate() expects, excluding the first and sixth element which will be Sun and CDT respectively, as well as re-ordering the year in the constructed string that is passed to CDate().

Dim input: input = "Sun Apr 05 00:00:00 CDT 2020"
Dim data: data = Split(input, Chr(32))
'Ignore first and sixth element in the array and build our date value
Dim output: output = CDate(data(1) & " " & data(2) & " " & data(5)) & " " & TimeValue(data(3))
Call Wscript.Echo(output)

Output:

05/04/2020 00:00:00

Note: Result will be based on the user's regional settings as CDate() uses this while parsing a Date string.


Useful Linkss

这篇关于无法将我的变量转换为 CDate 或 Serial Date,出现不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆