📅  最后修改于: 2021-01-11 14:28:22             🧑  作者: Mango
Date Format函数采用日期表达式,并返回一个字符串,该字符串包含根据说明格式化的表达式。
在Excel VBA中,Format函数是一个内置函数,它分为两个函数:
有两种方法可以在Excel VBA中格式化日期,例如:
Excel VBA日期格式遵循以下语法:
Format (expression, [format, [firstdayofweek, [firstweekofyear] ] ])
说明
Format | Explanation |
---|---|
General Date | It displays a date, which format is based on the system settings. |
Short Date | It displays a date, which format is based on the system’s short date setting. |
Medium Date | It displays a date, which format is based on the system’s medium date setting. |
Long Date | It displays a date, which format is based on the system’s long date setting. |
Short Time | It displays the time that depends on the system’s short time setting. |
Medium Time | It displays the time that depends on the system’s medium time setting. |
Long Time | It displays the time that depends on the system’s long time setting. |
Constant | Value | Explanation |
---|---|---|
vbUseSystem | 0 | It uses the NLS API setting. |
vbSunday | 1 | Sunday (default) |
vbMonday | 2 | Monday |
vbTuesday | 3 | Tuesday |
vbWednesday | 4 | Wednesday |
vbThursday | 5 | Thursday |
vbFriday | 6 | Friday |
vbSaturday | 7 | Saturday |
Constant | Value | Explanation |
---|---|---|
vbUseSystem | 0 | It uses the NLS API setting. |
vbFirstJan1 | 1 | It is a week that contains 1st January. |
vbFirstFourDays | 2 | The first week that has at least 4 days in a year. |
vbFirstFullWeek | 3 | The first full week of the year. |
步骤1:首先,单击“开发人员”选项卡,然后选择“ Visual Basic”选项。
第2步:点击插入按钮并创建一个新模块。
第3步:双击新添加的Module ,它将打开一个代码窗口。
步骤4:编写VBA日期格式函数代码,例如:
VBA日期格式函数分类为字符串类型的变量。
Dim用于声明变量名称及其类型。
在Format函数, Range函数用于获取特定单元格中的输出,这是短日期使用的最终代码格式。
第5步:现在,单击“运行”按钮并执行上面的代码,它将给出以下输出,例如:
示例2:我们还可以创建用户定义的日期格式,例如:
步骤1:在代码窗口中编写以下代码。
在上面的代码中,“格式”函数根据今天的日期应用于不同的用户定义日期格式。
步骤2:现在,单击“运行”按钮并执行上面的代码,它将给出以下代码,例如: