如何获得本年使用VBA
您好我正在使用这个过去的线程如何获得本年使用VBA
答案是使用Year(Date)
所以我这样实现:
Dim a As Integer a = Year(Date)
但是,当我试图使用它,我得到一个错误
Runtime 13 : Type Mismatch
在Excel VBA中,您想使用DATEPART
。 假设你试图获得年份的数据是有效的(这很难从你的问题中的less量信息中知道)。
从这里 :
MS EXCEL:DATEPARTfunction(VBA)
通过语法和示例学习如何使用Excel DATEPART函数。
描述
Microsoft Excel DATEPART函数返回给定date的指定部分。
句法
Microsoft Excel DATEPART函数的语法是:
DatePart(间隔,date,[firstdayofweek],[firstweekofyear])
这个具体的例子是:
DatePart(“yyyy”,“2012年10月15日”)将返回2012年
我怀疑你的types不匹配是因为Year(Date)
是有效的。 另一种select是使用Year(Now)
我怀疑你已经把你想要分配的variables声明为奇怪的东西,因为VBA会为你做很多奇怪的投射
Dim a As String a = Year(Now) ' "2014" Dim b As Double b = Year(Now) '2014 Dim c As Integer c = Year(Now) '2014 Dim d As Date d = Year(Date) '#7/6/1905# Dim e e = Year(Now) ' 2014 (implicitly cast to integer)
确保在模块的顶部设置了Option Explicit
,并编译代码。