好友
阅读权限10
听众
最后登录1970-1-1
|
老梁不说话
发表于 2026-3-31 20:10
以下是新代码,所有月份从上到下垂直排列在一个工作表里,使用起来更方便,不用切换Sheet。
Sub CreateSingleSheetIncomeCalendar()
Dim ws As Worksheet
Dim year As Integer, month As Integer
Dim rowStart As Long
Dim firstDay As Date
Dim startCol As Integer
Dim dayNum As Integer, i As Integer, c As Integer
Dim monthTitleRow As Long
Application.ScreenUpdating = False
' 创建或清空主工作表
On Error Resume Next
Application.DisplayAlerts = False
ThisWorkbook.Sheets("全年外快日历").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Set ws = ThisWorkbook.Sheets.Add
ws.Name = "全年外快日历"
rowStart = 2 ' 从第2行开始
For year = 2025 To 2027
For month = 1 To 12
monthTitleRow = rowStart
' 月份标题
ws.Cells(rowStart, 2).Value = year & "年 " & month & "月 每日外快收入记录"
ws.Cells(rowStart, 2).Font.Size = 16
ws.Cells(rowStart, 2).Font.Bold = True
ws.Range(ws.Cells(rowStart, 2), ws.Cells(rowStart, 8)).Merge
ws.Cells(rowStart, 2).HorizontalAlignment = xlCenter
rowStart = rowStart + 2
' 星期标题
ws.Cells(rowStart, 2).Value = "周一"
ws.Cells(rowStart, 3).Value = "周二"
ws.Cells(rowStart, 4).Value = "周三"
ws.Cells(rowStart, 5).Value = "周四"
ws.Cells(rowStart, 6).Value = "周五"
ws.Cells(rowStart, 7).Value = "周六"
ws.Cells(rowStart, 8).Value = "周日"
With ws.Range("B" & rowStart & ":H" & rowStart)
.Font.Bold = True
.Interior.Color = RGB(0, 102, 204)
.Font.Color = vbWhite
.HorizontalAlignment = xlCenter
End With
rowStart = rowStart + 1
firstDay = DateSerial(year, month, 1)
startCol = Weekday(firstDay, vbMonday) + 1 ' 周一从第2列开始
dayNum = 1
Dim currentRow As Long
currentRow = rowStart
For i = 1 To 42
c = ((i - 1) Mod 7) + 2
If i >= startCol And dayNum <= Day(DateSerial(year, month + 1, 0)) Then
' 日期
ws.Cells(currentRow, c).Value = dayNum
ws.Cells(currentRow, c).Font.Bold = True
ws.Cells(currentRow, c).HorizontalAlignment = xlCenter
' 收入输入格(日期下方)
ws.Cells(currentRow + 1, c).NumberFormat = "#,##0.00"
ws.Cells(currentRow + 1, c).Interior.Color = RGB(255, 255, 153) ' 浅黄色
dayNum = dayNum + 1
End If
If c = 8 Then
currentRow = currentRow + 3 ' 日期行 + 收入行 + 间隔行
End If
Next i
rowStart = currentRow + 3 ' 下一个月留点间隔
Next month
Next year
ws.Columns("B:H").ColumnWidth = 14
ws.Rows.RowHeight = 22
ws.Range("A1").Select
Application.ScreenUpdating = True
MsgBox "单个工作表全年日历创建完成!(2025-2027)", vbInformation
End Sub |
|