Need help with my code. I have two issues.
1st issue is I try to make my code to loop if fulfill empty cell and date at column 1 less than today date
And .Cells(rw, 1) < Format(dt, "dd-mmm")
but this < today date not able to work. The macro still continue even date is greater than today date2nd issue is where i would like to autofill from column 5 to 9 from previous row. I keep encounter range error
Set rng = .Range(.Cells(rw - 1, 5), .Cells(rw, 9)) Selection.AutoFill Destination:=rng, Type:=xlFillDefault
andSelection.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")
with.Cells(rw, 1) < CLng(dt)
. Excel doesn't care about the date format. It keeps the date asLong
... – FaneDuruAutofill method of Range class failed
– LeoEY.Cells(rw, 1) < CLng(dt)
yes, this help. Thanks so much. – LeoEY