Relative Reference and AutoFill- Macro

G

Guest

I need to copy a formula in a new column for each row. When recording the
macro I am using the autofill function however each time I use this
spreadsheet there will be a different number of rows. I need the autofill to
adapt. The following is part of the script I think needs adjustment I just
don't know how to adjust it. Thanks in advance for any ideas.

Range("H2").Select
Selection.AutoFill Destination:=Range("H2:H423")
Range("H2:H423").Select
 
G

Guest

dim endRow as long
endRow = cells(rows.count,7).end(xlup).row
Range("H2").AutoFill Destination:=Range("H2:H" &endrow)

should give your the required result.

Regards
Rowan
 
G

Guest

I also didn't type Cells with a capital C, leave a space between the , and 7
or use a capital U in xlUp etc etc but that is the wonderfull thing about the
VBE: it'll do all of that for me. <g>
 
G

Guest

I deliberately use a capital letter in my variable names and leave out spaces
and when I move to next line if VB doesn’t correct proper case and space I
know I have a error.
 
G

Guest

Hi Rowan

Your code is really useful. However i have a similar situation but the
starting cell to copy down using autofill might not be H2, might be H3, H5,
H100.

Can I use the actively selected cell to perform the autofill down? let say
it is in the H column?

Thanks you very much!

Leung
 
T

Tom Ogilvy

dim endRow as long
endRow = cells(rows.count,ActiveCell.Column).end(xlup).row
ActiveCell.AutoFill
Destination:=Range(Activecell,Cells(endrow,ActiveCell.Column))

or
dim EndCell as Range
set EndCell = cells(rows.count,ActiveCell.Column).End(xlup)
ActiveCell.AutoFill Destination:=Range(ActiveCell,EndCell)
 
G

Guest

Thanks Tom, however, it gives this error
Run-time error '1004'
AutoFill method of Range class failed

my situation is that.. as below thomas and peter pre-typed data but Leung is
a vlookup result.. i put it there programmatically to look on other named
range.
i want to copy down the formula from Leung cell to down, but I don't know
how much more cell is required to autofill to the destination.

I have treid to run the code by putting the selection in Leung but get this
error.

A B
Period Name type
2004 Thomas A
2004 Peter B
2004 Leung C
2004 B
2004 A
 

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