I'm making a process control sheet to practice vba but I've found out that the chunk I've coded doesn't work well.
What I want is a calendar which is automatically printing months, days, and dates next to next from the year I've written in specific cell.
like this
but the result is that the day and date are overwritten in only one cell.
I don't know why it doesn't work.
Public Sub automateCalendar()
Dim i As Integer
Dim j As Integer
Dim checkYear As Integer
Dim lastDay As Integer
checkYear = Cells(1, "B").Value
lastDay = Day(DateSerial(checkYear, i + 1, 0))
Dim lastDayColumn As Long
Dim lastMonthColumn As Long
Dim dateCheck As String
Application.DisplayAlerts = False
For i = 1 To 12
For j = 1 To lastDay
lastDayColumn = Cells(4, Columns.Count).End(xlToLeft).Column
Cells(4, lastDayColumn + 1).Value = j
If j = 1 Then
Cells(4, lastDayColumn + 1).Offset(-1, 0).Value = i
lastMonthColumn = Cells(3, Columns.Count).End(xlToLeft).Column
End If
If j = lastDay Then
With Range(Cells(3, lastMonthColumn), Cells(3, lastDayColumn + 1).Offset(-1, 0))
.Merge
.Font.Bold = True
.Font.Size = 20
.HorizontalAlignment = xlCenter
End With
With Columns(lastDayColumn).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
End If
dateCheck = Format(DateSerial(checkYear, i, j), "aaa")
If dateCheck = "Sat" Or dateCheck = "Sun" Then
Cells(4, lastDayColumn + 1).Font.Color = vbRed
With Cells(4, lastDayColumn + 1).Offset(1, 0)
.Value = dateCheck
.Font.Color = vbRed
End With
Else
Cells(4, lastDayColumn + 1).Offset(1, 0).Value = dateCheck
End If
Next j
Next i
Application.DisplayAlerts = True
End Sub
Thank you all for your help and concern for my question, first of all.
I haven't noticed that vba cannot find the lastcolumn which contains something and I want to set as variable when the last column is hidden.
Only after turning off the hidden property of columns which contain last column, it works well again.
I could have know this fact. :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.