如何在Excel中创建日历?

如何在Excel中创建日历?

使用以下VBA代码,您可以快速创建月历。请按如下操作:

1. 按Alt + F11键打开Microsoft Visual Basic for Applications窗口。

2. 将显示一个新窗口。点击插入 > 模块,然后在模块中输入以下代码:

Sub CalendarMaker()

ActiveSheet.Protect DrawingObjects:=False, Contents:=False, _

Scenarios:=False

Application.ScreenUpdating = False

On Error GoTo MyErrorTrap

Range("a1:g14").Clear

MyInput = InputBox("Type in Month and year for Calendar ")

If MyInput = "" Then Exit Sub

StartDay = DateValue(MyInput)

If Day(StartDay) <> 1 Then

StartDay = DateValue(Month(StartDay) & "/1/" & _

Year(StartDay))

End If

Range("a1").NumberFormat = "mmmm yyyy"

With Range("a1:g1")

.HorizontalAlignment = xlCenterAcrossSelection

.VerticalAlignment = xlCenter

.Font.Size = 18

.Font.Bold = True

.RowHeight = 35

End With

With Range("a2:g2")

.ColumnWidth = 11

.VerticalAlignment = xlCenter

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

.Orientation = xlHorizontal

.Font.Size = 12

.Font.Bold = True

.RowHeight = 20

End With

Range("a2") = "Sunday"

Range("b2") = "Monday"

Range("c2") = "Tuesday"

Range("d2") = "Wednesday"

Range("e2") = "Thursday"

Range("f2") = "Friday"

Range("g2") = "Saturday"

With Range("a3:g8")

.HorizontalAlignment = xlRight

.VerticalAlignment = xlTop

.Font.Size = 18

.Font.Bold = True

.RowHeight = 21

End With

Range("a1").Value = Application.Text(MyInput, "mmmm yyyy")

DayofWeek = Weekday(StartDay)

CurYear = Year(StartDay)

CurMonth = Month(StartDay)

FinalDay = DateSerial(CurYear, CurMonth + 1, 1)

Select Case DayofWeek

Case 1

Range("a3").Value = 1

Case 2

Range("b3").Value = 1

Case 3

Range("c3").Value = 1

Case 4

Range("d3").Value = 1

Case 5

Range("e3").Value = 1

Case 6

Range("f3").Value = 1

Case 7

Range("g3").Value = 1

End Select

For Each cell In Range("a3:g8")

RowCell = cell.Row

ColCell = cell.Column

If cell.Column = 1 And cell.Row = 3 Then

ElseIf cell.Column <> 1 Then

If cell.Offset(0, -1).Value >= 1 Then

cell.Value = cell.Offset(0, -1).Value + 1

If cell.Value > (FinalDay - StartDay) Then

cell.Value = ""

Exit For

End If

End If

ElseIf cell.Row > 3 And cell.Column = 1 Then

cell.Value = cell.Offset(-1, 6).Value + 1

If cell.Value > (FinalDay - StartDay) Then

cell.Value = ""

Exit For

End If

End If

Next

For x = 0 To 5

Range("A4").Offset(x * 2, 0).EntireRow.Insert

With Range("A4:G4").Offset(x * 2, 0)

.RowHeight = 65

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlTop

.WrapText = True

.Font.Size = 10

.Font.Bold = False

.Locked = False

End With

With Range("A3").Offset(x * 2, 0).Resize(2, _

7).Borders(xlLeft)

.Weight = xlThick

.ColorIndex = xlAutomatic

End With

With Range("A3").Offset(x * 2, 0).Resize(2, _

7).Borders(xlRight)

.Weight = xlThick

.ColorIndex = xlAutomatic

End With

Range("A3").Offset(x * 2, 0).Resize(2, 7).BorderAround _

Weight:=xlThick, ColorIndex:=xlAutomatic

Next

If Range("A13").Value = "" Then Range("A13").Offset(0, 0) _

.Resize(2, 8).EntireRow.Delete

ActiveWindow.DisplayGridlines = False

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, _

Scenarios:=True

ActiveWindow.WindowState = xlMaximized

ActiveWindow.ScrollRow = 1

Application.ScreenUpdating = True

Exit Sub

MyErrorTrap:

MsgBox "You may not have entered your Month and Year correctly." _

& Chr(13) & "Spell the Month correctly" _

& " (or use 3 letter abbreviation)" _

& Chr(13) & "and 4 digits for the Year"

MyInput = InputBox("Type in Month and year for Calendar")

If MyInput = "" Then Exit Sub

Resume

End Sub 3. 然后点击运行 按钮或按F5键运行应用程序。现在会弹出一个提示框,您可以在空白框中输入月份和年份。

结果

相关推荐

余震是什么?
365bet足球盘

余震是什么?

07-21 👁️ 4232
美术纸4开尺寸是多少?标准规格解析与选购指南
详细指南:如何在PDF文件中插入图片的步骤
365bet体育在线中文

详细指南:如何在PDF文件中插入图片的步骤

07-30 👁️ 2486
市场监管总局依法对阿里巴巴集团控股有限公司在中国境内网络零售平台服务市场实施
1986年世界杯决赛:西徳vs阿根廷,马拉多纳的传奇之战
完美体育365官方网站

1986年世界杯决赛:西徳vs阿根廷,马拉多纳的传奇之战

08-06 👁️ 7800
1986年世界杯决赛阿根廷对
完美体育365官方网站

1986年世界杯决赛阿根廷对

07-07 👁️ 6322
单机版激活
完美体育365官方网站

单机版激活

07-20 👁️ 1360
免费好用的画画软件有哪些?试试这7个!
完美体育365官方网站

免费好用的画画软件有哪些?试试这7个!

07-24 👁️ 1202
蕲的五笔怎么打?蕲的五笔拆分图.
365bet足球盘

蕲的五笔怎么打?蕲的五笔拆分图.

06-29 👁️ 538