Autofill in VBA question

  • Thread starter Thread starter CarlosAntenna
  • Start date Start date
C

CarlosAntenna

I am not a VBA programmer by any stretch of the imagination. I am here to
ask for help with a macro that I _recorded_.

Part of what my macro does is:

Insert a column in a sheet of data
Type a formula into the top cell of the new column
Copy the formula down the column for as far as there is data

I did this last step by double clicking the fill handle and this is the code
that is generated

Range("B4").Select
Selection.AutoFill Destination:=Range("B4:B450")
Range("B4:B450").Select

That's OK this time, but what about when there is more or less lines of
data.

How can this be changed to accomplish what I am looking for?

Thanks,
Carlos
 
Hi Carlos

Try this

Will fill B1 till the row of the last cell with data in A

Sub test2()
Dim LastRow As Long
With Worksheets("Sheet1")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("B1").AutoFill Destination:=.Range("B1:B" & LastRow) _
, Type:=xlFillDefault
End With
End Sub
 
CarlosAntenna said:
I am not a VBA programmer by any stretch of the imagination. I am here to
ask for help with a macro that I _recorded_.

Part of what my macro does is:

Insert a column in a sheet of data
Type a formula into the top cell of the new column
Copy the formula down the column for as far as there is data

I did this last step by double clicking the fill handle and this is the code
that is generated

Range("B4").Select
Selection.AutoFill Destination:=Range("B4:B450")
Range("B4:B450").Select

That's OK this time, but what about when there is more or less lines of
data.

How can this be changed to accomplish what I am looking for?

Thanks,
Carlos
 
one way:

Sub test()
Dim lRow As Long
lRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Range("B4:B" & lRow).FillDown
End Sub
 

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

Similar Threads

Autofill macro question 5
Autofill Error 4
Autofil macro 11
Flexible autofill in macro 3
Autofill with Column as a variable 5
Autofill range 2
Autofil syntax to auto-complete the range 3
Error 1004 - Autofill 3

Back
Top