需要一些幫助,這行代碼:Excel的VBA自動填充目標
.Range("A1:G1").AutoFill Destination:=.Range("A1:U1")
我試圖自動化製作日曆。如果將範圍更改爲除A1:U1
以外的任何值,代碼將無法編譯。我想延長至A1:AE1
任何原因,它被卡住,沒有編制呢?
謝謝!
Sub CreateCalendar()
Dim lMonth As Long
Dim strMonth As String
Dim rStart As Range
Dim strAddress As String
Dim rCell As Range
Dim lDays As Long
Dim dDate As Date
'Add new sheet and format
ActiveWindow.DisplayGridlines = True
With Cells
.ColumnWidth = 6#
.Font.Size = 8
End With
'Create the Month headings
For lMonth = 1 To 12
Select Case lMonth
Case 1
strMonth = "January"
Set rStart = Range("A1")
Case 2
strMonth = "February"
Set rStart = Range("A3")
Case 3
strMonth = "March"
Set rStart = Range("A5")
Case 4
strMonth = "April"
Set rStart = Range("A7")
Case 5
strMonth = "May"
Set rStart = Range("A9")
Case 6
strMonth = "June"
Set rStart = Range("A11")
Case 7
strMonth = "July"
Set rStart = Range("A13")
Case 8
strMonth = "August"
Set rStart = Range("A15")
Case 9
strMonth = "September"
Set rStart = Range("A17")
Case 10
strMonth = "October"
Set rStart = Range("A19")
Case 11
strMonth = "November"
Set rStart = Range("A21")
Case 12
strMonth = "December"
Set rStart = Range("A23")
End Select
'Merge, AutoFill and align months
With rStart
.Value = strMonth
.HorizontalAlignment = xlCenter
.Interior.ColorIndex = 6
.Font.Bold = True
With .Range("A1:G1")
.Merge
.BorderAround LineStyle:=xlContinuous
End With
**.Range("A1:G1").AutoFill Destination:=.Range("A1:U1")**
End With
Next lMonth
'Pass ranges for months
For lMonth = 1 To 12
strAddress = Choose(lMonth, "A2:AE2", "A4:AE4", "A6:AE6", _
"A8:AE8", "A10:AE10", "A12:AE12", _
"A14:AE14", "A16:AE16", "A18:AE18", _
"A20:AE20", "A22:AE22", "A24:AE24")
lDays = 0
Range(strAddress).BorderAround LineStyle:=xlContinuous
'Add dates to month range and format
For Each rCell In Range(strAddress)
lDays = lDays + 1
dDate = DateSerial(Year(Date), lMonth, lDays)
If Month(dDate) = lMonth Then ' It's a valid date
With rCell
.Value = dDate
.NumberFormat = "ddd dd"
End With
End If
Next rCell
Next lMonth
'add con formatting
With Range("A1:AE28")
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=TODAY()"
.FormatConditions(1).Font.ColorIndex = 2
.FormatConditions(1).Interior.ColorIndex = 1
End With
End Sub
那是什麼,你得到實際的錯誤信息? – Michael
它不會工作,是因爲AE犯規適合在合併單元格範圍內,當你嘗試自動填充,如果你選擇AB或AI將工作.. AE是列31,你已經合併了7個細胞的原因,由7月31日犯規鴻溝(至少不是全部數字)..選擇一個可以被7整除的單元格,並且你很好走 –
我仍然收到1004錯誤。我可能無法解釋清楚,我想要在28-31列之間合併(最大),以便在日期之間進行分隔。 – Collin