Error 1004 - Autofill

D

diepvic

Hi,
I've got a macro like below:
lngLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("M2:X2").Select
Selection.AutoFill Destination:=Range("M2:X" & lngLastRow)

When I run the macro, I come up with error 1004 - Autofill method of range
class failed.

the lngLastRow is 2.

Can anyone help me?
Thanks
 
P

Per Jessen

Hi

Your code is working perfectly, the issue is that lngLastRow is two, so
there are no rows to fill.

If you have some data in A3, and run the macro it will run with no error.

Hopes this helps.

Per
 
D

diepvic

Thanks so much Per Jessen,

Do you have any idea to solve this? The data can have one row or more than
that. I can not predict it.

Should I use the If ...then...else statement?
like, If lngLastRow >2 then
Range("M2:X2").Select
Selection.AutoFill Destination:=Range("M2:X" & lngLastRow)

Thanks
 
P

Per Jessen

Using a If ...Then statement like you suggest is the way to solve it, but
you don't need to select the range.

If lngLastRow > 2 Then
Range("M2:X2").AutoFill Destination:=Range("M2:X" & lngLastRow)
End If

Regards,
Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top