如何使用VBA获得当前年份 [英] How to get current Year Using VBA

查看:246
本文介绍了如何使用VBA获得当前年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用过去的线程如何获取当前年份使用Vba

Hi I am using this past thread How To Get The Current Year Using Vba

答案是使用 Year(Date),所以我是这样实现的:

The answer was to use Year(Date) so I implemented it like this:

Dim a As Integer
a = Year(Date)

但是当我尝试使用它时,出现错误

But when I tried to use it, I am getting an error

运行时13:键入不匹配

推荐答案

在Excel VBA中,您要使用 DATEPART .假设您尝试获取年份的数据是有效的(很难从问题中的少量信息中得知).

In Excel VBA, you want to use DATEPART. Assuming that the data that you're trying to get the year from is valid (which is hard to know from the little information in your question).

此处:

MS EXCEL:DATEPART功能(VBA)

了解如何结合使用Excel DATEPART函数和语法和示例.

Learn how to use the Excel DATEPART function with syntax and examples.

说明

Microsoft Excel DATEPART函数返回以下内容的指定部分:给定日期.

The Microsoft Excel DATEPART function returns a specified part of a given date.

语法

Microsoft Excel DATEPART函数的语法为:

The syntax for the Microsoft Excel DATEPART function is:

DatePart(时间间隔,日期,[一周的第一天],[一年的第一周])

DatePart( interval, date, [firstdayofweek], [firstweekofyear] )

和此特定示例:

DatePart("yyyy","15/10/2012")将返回2012

DatePart("yyyy", "15/10/2012") would return 2012

这篇关于如何使用VBA获得当前年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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