【发布时间】:2020-02-13 08:19:34
【问题描述】:
需要有关我的代码的帮助。我有两个问题。
第一个问题是我尝试让我的代码循环,如果第 1 列的空单元格和日期小于今天的日期
And .Cells(rw, 1) < Format(dt, "dd-mmm")
但是这个第二个问题是我想从前一行的第 5 列自动填充到第 9 列。我一直遇到范围错误
Set rng = .Range(.Cells(rw - 1, 5), .Cells(rw, 9)) Selection.AutoFill Destination:=rng, Type:=xlFillDefault
和Selection.AutoFill Destination:=Range(.Cells(rw - 1, 5), .Cells(rw 1, 5)), Type:=xlFillDefault
Sub DataGrab()
Dim rw As Long, x As Range, rng As Range
Dim extwbk As Workbook, twb As Workbook
Set twb = ThisWorkbook
Set extwbk = Workbooks.Open("D:\Users\Desktop\Report " & Format(Now, "DD-MMM-YYYY") & ".xls")
Set x = extwbk.Worksheets("Summary").Range("A4:AF100")
dt = Date
With twb.Sheets("Sheet1")
For rw = 3 To .Cells(Rows.Count, 1).End(xlUp).Row
If .Cells(rw, 2) = "" And .Cells(rw, 1) < Format(dt, "dd-mmm") Then
.Cells(rw, 2) = Application.VLookup(.Cells(rw, 1).Value2, x, 23, False)
.Cells(rw, 3) = Application.VLookup(.Cells(rw, 1).Value2, x, 29, False)
.Cells(rw, 4) = Application.VLookup(.Cells(rw, 1).Value2, x, 31, False)
Set rng = .Range(.Cells(rw - 1, 5), .Cells(rw, 9))
Selection.AutoFill Destination:=rng, Type:=xlFillDefault
'Selection.AutoFill Destination:=Range(.Cells(rw - 1, 5), .Cells(rw 1, 5)), Type:=xlFillDefault
End If
Next rw
End With
extwbk.Close savechanges:=False
End Sub
【问题讨论】:
-
尝试将
.Cells(rw, 1) < Format(dt, "dd-mmm")更改为.Cells(rw, 1) < CLng(dt)。 Excel 不关心日期格式。它将日期保持为Long... -
@DirkReichel 仍然遇到
Autofill method of Range class failed -
@FaneDuru
.Cells(rw, 1) < CLng(dt)是的,这个帮助。非常感谢。 -
@LeoEY:很高兴为您提供帮助!我会将我的评论转移到答案...如果您勾选代码左侧的复选框,这会将其设置为接受的答案...
-
@FaneDuru 非常感谢。是的,标记在评论上打勾。剩下的第二个问题要排查