直接上代码~~

1.  在一个 Excel 生成当月或当年指定月份的日期及星期

' 获取星期的显示
Function disp(i As Integer)
Select Case i
Case
disp = "一"
Case
disp = "二"
Case
disp = "三"
Case
disp = "四"
Case
disp = "五"
Case
disp = "六"
Case Else
disp = "日"
End Select
End Function ' 获取当月的天数
Function GetDaysOfMonth(Year As String, Month As String) As Integer
Dim Day1, Day2 As String
If Month = "" Then
GetDaysOfMonth =
Else
Day1 = Year + "-" + Month + "-1"
Day2 = Year + "-" + CStr(CInt(Month) + ) + "-1"
GetDaysOfMonth = DateDiff("d", Day1, Day2)
End If
End Function Sub AddSheets()
Dim i As Integer
Dim DaysOfMonth As Integer
Dim NameStr As String
Dim DateStr As String
Dim CurrMonth As Integer
Dim MonStr As String
Dim CurrYear As String
Dim Choice As Integer
Dim LastMonth As Integer
Dim OriginSheet As String Application.DisplayAlerts = False
For i = Sheets.Count To Step -
If Sheets(i).Name <> ActiveSheet.Name Then
Sheets(i).Delete
End If
Next ActiveSheet.Name = "LastSheet"
OriginSheet = ActiveSheet.Name CurrMonth = CInt(Month(Now)) ' 设置起始及结束月份(1-12); 默认当前月
StartMonth = CurrMonth
LastMonth = CurrMonth CurrYear = CStr(Year(Now))
For m = StartMonth To LastMonth
MonStr = CStr(m)
DaysOfMonth = GetDaysOfMonth(CurrYear, MonStr)
For i = To DaysOfMonth
Worksheets.Add after:=Worksheets(Worksheets.Count)
NameStr = MonStr & "-" & CStr(i)
DateStr = CurrYear & "-" & NameStr
ActiveSheet.Name = NameStr
ActiveSheet.[A1].Value = DateStr
ActiveSheet.[B1].Value = "星期" & disp(Weekday(DateStr, vbMonday)) ' 设置单元格行列宽高自适应
ActiveSheet.[A1].Columns.AutoFit
ActiveSheet.[A1].Rows.AutoFit
ActiveSheet.[B1].Columns.AutoFit
ActiveSheet.[B1].Rows.AutoFit
Next
Next
Sheets(OriginSheet).Delete
On Error Resume Next
Application.DisplayAlerts = True
End Sub

2.  生成直到2099年的日期及月份,每个月份一个 Excel

' 获取星期的显示
Function disp(i As Integer)
Select Case i
Case
disp = "一"
Case
disp = "二"
Case
disp = "三"
Case
disp = "四"
Case
disp = "五"
Case
disp = "六"
Case Else
disp = "日"
End Select
End Function ' 获取当月的天数
Function GetDaysOfMonth(Year As String, Month As String) As Integer
Dim Day1, Day2 As String
If Month = "" Then
GetDaysOfMonth =
Else
Day1 = Year + "-" + Month + "-1"
Day2 = Year + "-" + CStr(CInt(Month) + ) + "-1"
GetDaysOfMonth = DateDiff("d", Day1, Day2)
End If
End Function Sub AddSheets(Year As String, Month As String)
Dim i As Integer
Dim DaysOfMonth As Integer
Dim NameStr As String
Dim DateStr As String
Dim CurrMonth As Integer
Dim MonStr As String
Dim OriginSheet As String For i = Sheets.Count To Step -
If Sheets(i).Name <> ActiveSheet.Name Then
Sheets(i).Delete
End If
Next ActiveSheet.Name = "LastSheet"
OriginSheet = ActiveSheet.Name MonStr = CStr(Month)
DaysOfMonth = GetDaysOfMonth(Year, MonStr)
For i = To DaysOfMonth
Worksheets.Add after:=Worksheets(Worksheets.Count)
NameStr = MonStr & "-" & CStr(i)
DateStr = Year & "-" & NameStr
ActiveSheet.Name = NameStr
ActiveSheet.[A1].Value = DateStr
ActiveSheet.[B1].Value = "星期" & disp(Weekday(DateStr, vbMonday)) ' 设置单元格行列宽高自适应
ActiveSheet.[A1].Columns.AutoFit
ActiveSheet.[A1].Rows.AutoFit
ActiveSheet.[B1].Columns.AutoFit
ActiveSheet.[B1].Rows.AutoFit
Next
Sheets(OriginSheet).Delete
On Error Resume Next End Sub Sub AddExcels(Year As String)
Dim wb As Workbook
Dim wbname As String
Dim m As Integer
Dim Month As String For m = To
Set wb = Workbooks.Add
Month = CStr(m)
Call AddSheets(Year, Month)
wbname = Year & "年" & CStr(Month) & "月.xlsx"
wb.SaveAs "d:\" & wbname
Workbooks(wbname).Close (True)
Next End Sub Sub AddExcels2099()
Dim Year As Integer Application.DisplayAlerts = False
For Year = To
AddExcels (CStr(Year))
Next
Workbooks(ActiveWorkbook.Name).Close (False)
Application.DisplayAlerts = True End Sub

小记:

(1)  函数返回值,使用函数名作为变量在最后一行赋值;

(2)  调用过程: CALL SubName(ArgList) ;

(3)  变量名、函数名习惯大写;

(4)  Switch , If, For , Sub, Function 定义代码里有;

(5)  整数转字符串 CStr,  字符串转整数 CInt ; 字符串连接 & ;

(6)  当前活动工作表 ActiveSheet , 当前活动工作簿: ActiveWorkBook ;

(7)  操作当前活动工作表: ActiveSheet.Name,  ActiveSheet.[CellID].Value ; ActiveSheet.[A1].Columns, ActiveSheet.[A1].Rows 行列设置;

(8)  工作簿操作:  新增 Set wb = Workbooks.Add ; 保存 wb SaveAs "Path/file.xlsx" ;  关闭  Workbooks(wbname).Close (True) .

无论怎样的编程语言, 函数或过程复用是最基本的技能;

只要是在计算设备上, 99%的人工操作均可自动化。

VBA 操作 Excel 生成日期及星期的更多相关文章

  1. [转] C#操作EXCEL,生成图表的全面应用

    gailzhao 原文 关于C#操作EXCEL,生成图表的全面应用 近来我在开发一个运用C#生成EXCEL文档的程序,其中要根据数据生成相应的图表,该图表对颜色和格式都有严格的要求,在百度和谷歌中搜索 ...

  2. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  3. Excel vba:批量生成超链接,添加边框,移动sheet等

    Excel vba 操作 批量生成sheet目录并添加超链接 Sub Add_Sheets_Link() 'Worksheets(5)为清单目录页 '在sheet页上生成sheet页名字并超链接 To ...

  4. VBA在Excel中的应用(三)

    目录  Chart Export Chart Format Chart Lengend  Chart Protect  Chart Title  Chart Chart Export 1. 将Exce ...

  5. 使用Win32::OLE操作Excel——Excel对象模型

    像VBA操作Excel一样,Win32::OLE模块也是通过对象操作来控制Excel. 如果想自动化操作和控制Excel应用程序,则必须要与Excel对象模型所提供的对象进行交互.理解和熟悉Excel ...

  6. VC操作Excel之基本操作(颜色等)【转载】

    文章出处https://blog.csdn.net/form88/article/details/78566390 EXCEL里如何知道某种颜色的ColorIndex的值 ===fag::====== ...

  7. Java生成和操作Excel文件(转载)

    Java生成和操作Excel文件   JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该A ...

  8. Java生成和操作Excel文件

    JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...

  9. Excel VBA 操作 Word(入门篇)

    原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人.   一.新建Word引用 需要首先创建一个对 Word A ...

随机推荐

  1. 抄书 Copying Books UVa 714

    Copying  Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...

  2. hdu 2102 BFS

    原题链接 思路:bfs搜一发 AC代码: #include "map" #include "queue" #include "math.h" ...

  3. java web(四)文件上传与下载

     一.文件上传原理 1.在TCP/IP中,最早出现的文件上传机制是FTP ,它是将文件由客户端发送到服务器的标准机制:但是在jsp使用过程中不能使用FTP方法上传文件,这是由jsp运行机制所决定. 通 ...

  4. oracle 连接查询,和(+)符号的用法

    --连接查询 左链接.右链接,全链接 --内链接select e.account 用户名, e.empname 名称, c.comname 公司名称  from employee e inner jo ...

  5. asp.net 获取当前项目的根目录路径

    获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables["PATH_INFO"]) Server.MapPath(" ...

  6. 抽象工厂模式(Abstract Factory)

    GOF:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图:

  7. Android课程---计算器的实现

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  8. MySQL 启动时禁用了 InnoDB 引擎的解决方法

    今天在从本地数据库复制表数据到虚拟机 CentOS 6.6 上的数据库时,得到提示: Unknown table engine 'InnoDB' 于是在服务器 MySQL 中查看了引擎: mysql& ...

  9. 打包成zip

    protected void btnExportZip_Click(object sender,EventArgs e) { string archiveName=String.Format(&quo ...

  10. xib连线出错,模型保存cell状态(最后个Cell隐藏分割线),

    一个.m文件中有好几个cell类,拖线,要看看该控件对应的是哪个类,否则点击事件不响应,因为归属的xib错了 拖不过来线,因为是view拖不动,加了个button就行了   使用模型属性记录是否隐藏c ...