Problem with formula autofill code

banjoflanjo

New Member
Joined
Mar 20, 2008
Messages
44
Hi,

I'm currently using the following code for autofilling a formula via a macro:

Dim rng As Range
Set rng = ActiveSheet.Range("B9")
rng.AutoFill Range(rng, rng.Offset(0, -1).End(xlDown).Offset(0, 1))

Basically, I run a standard weekly report and use a macro to automatically insert formulae, chop up and filter the data as required, and then drop the formatted info into a database. Now the above code has been working great until the last report that I ran. The last report only had one line of data on it for the week, so my macro inserted the formulae against the one line of info, and then tried to fill all of my formulae down to the very last row and caused problems with the rest of my macro from there.

Does anyone know how I can do the same as the code used above, but so that it can also recognise when there is only one line of data and so that it doesn't try to fill down to the very bottom?

Many thanks in advance!

Banjo
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello,

how about

Code:
Dim rng As Range
    Set rng = ActiveSheet.Range("B9")
    If Range("A65536").End(xlUp).Row > 9 Then
        rng.AutoFill Range(rng, rng.Offset(0, -1).End(xlDown).Offset(0, 1))
    End If
 
Upvote 0
Thanks onlyadrafter,

That worked a treat!!

However I've now got another related problem. In my macro I use the code:

Range("CopyStart1:CopyEnd1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

This is the bit where I pick up my final data to drop it into the database. CopyStart1 and CopyEnd1 are just the start and end of the first row of data, the rest takes me to the bottom of the block of data and copies it. However, I'v now only got one row of data for the week. So when I come to move to the bottom of the data block it goes straight to row 65536.

Any ideas anyone?????
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top