[英]how to fix my nested for statement in vba?
我正在制作一個過程控制表來練習vba,但我發現我編碼的代碼塊無法正常工作。
我想要的是一個日歷,該日歷會自動打印我在特定單元格中寫入的年份的下一個月,日和下一個日期。
像這樣
但是結果是日期和日期僅在一個單元格中被覆蓋。
我不知道為什么它不起作用。
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
首先,感謝大家對我的問題的幫助和關注。
我沒有注意到vba找不到包含某些內容的lastcolumn,並且我想在隱藏最后一列時將其設置為變量。
僅在關閉包含最后一列的列的hidden屬性后,它才能再次正常工作。
我早就知道這個事實。 :(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.