Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autofill Code 1

Status
Not open for further replies.

luceze

Programmer
Apr 26, 2001
842
US
Does anyone know how to write a macro that will autofill a range if there is data in the cell to the left. (the same as double clicking on the autofill square)
I have this code
Selection.AutoFill Destination:=Range("a1:a5")
which works but I have to state the range.

If anyone understands what I'm talking about I would appreciate the help.

Eric
 
I thought using a Selection.Offset in the Destination might work, but it just gave me an error. You may have to use a Copy/PasteSpecial routine instead of Autofill. Use the Offset property to select where to paste the formula.
 
Eric,

Here's a routine which works. I avoided using the "AutoFill" option because the "Source" cell must be included in the "Destination", plus I had other difficulties. With my method, it requires that you have a cell (off to the side somewhere) which is formattedd with your fill of choice, and you need to name the cell. I have used the name "filltype" in the following routine.

As requested, the routine first checks for a value in the cell to the left of the cursor.

Sub Fill_Cells()
num = ActiveCell.Offset(0, -1).Value
If num = "" Then Exit Sub
Range("filltype").Copy
curcell = ActiveCell.Address
ActiveCell.Offset(4, 0).Select
botcell = ActiveCell.Address
fillRange = curcell & ":" & botcell
Range(fillRange).Select
ActiveSheet.Paste
End Sub

Hope this works to your satisfaction.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Thanks Dale. As usual you were right on the mark.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top