PC Review


Reply
Thread Tools Rate Thread

copying formulae down a column

 
 
Colin Foster
Guest
Posts: n/a
 
      2nd Jan 2008
Hi,

I have an Excel 2003 spreadsheet which retrieves the majority of its data
from a Query; the spreadsheet then needs to perform a number of calculations
on this data.

If I select the "Fill Down Formulas in columns adjacent to data" property,
then whilst the formulas fill the first time, when the query is refreshed
then the formulas stop working - some have a #REF value & others start
referencing, for example, Row 497 (which is where the previous "version" of
the query's results extended to).

So, two questions...

First of all, am I doing "something wrong" with the query parameters, or is
my error typical?

Secondly, as an alternative, I was going to create a macro to copy the
formulae down to the current last row, but I'm not sure of what code to use,
so any help would be very welcome.

Regards
Colin Foster
ps... Happy New Year!!

 
Reply With Quote
 
 
 
 
Dan R.
Guest
Posts: n/a
 
      2nd Jan 2008
Colin,

Here's an example assuming your formula is in cell B1:

Sub fillToLastRow()
Dim lastRow As Long, fillRange As Range

lastRow = Range("A65536").End(xlUp).Row
Set fillRange = Range(Cells(1, 2), Cells(lastRow, 2))

Range("B1").AutoFill fillRange
End Sub

--
Dan


On Jan 2, 11:53*am, Colin Foster
<ColinFos...@discussions.microsoft.com> wrote:
> Hi,
>
> I have an Excel 2003 spreadsheet which retrieves the majority of its data
> from a Query; the spreadsheet then needs to perform a number of calculations
> on this data.
>
> If I select the "Fill Down Formulas in columns adjacent to data" property,
> then whilst the formulas fill the first time, when the query is refreshed
> then the formulas stop working - some have a #REF value & others start
> referencing, for example, Row 497 (which is where the previous "version" of
> the query's results extended to).
>
> So, two questions...
>
> First of all, am I doing "something wrong" with the query parameters, or is
> my error typical?
>
> Secondly, as an alternative, I was going to create a macro to copy the
> formulae down to the current last row, but I'm not sure of what code to use,
> so any help would be very welcome.
>
> Regards
> Colin Foster
> ps... Happy New Year!!

 
Reply With Quote
 
Charles Chickering
Guest
Posts: n/a
 
      2nd Jan 2008
Colin, while I'm no expert on query's, this does sound normal to me. It sound
like when the query updates it is moving the cells containing the resulting
data. Excel attempts to be intelligent (and fails miserably) by tracking the
cell that it was originally referenced to. While a macro would work, the
"Offset" command might work for you. I learned this trick from Dick Kusleika
at "Daily Dose of Excel"
http://www.dailydoseofexcel.com/

For your formula, instead of using the address of the cells in the query
data, use an "Offset" of the cell it resides in. For example this would be a
formula to sum columns "A" through "G" with the formula residing in column
"H":
=SUM(OFFSET(H1,0,-7):OFFSET(H1,0,-1))
Thus the only "refereneced" cell is the cell that the formula itself resides
in, the data can be moved, deleted etc. and the formula shouldn't break.

If you want to write a macro anyhow, this should get you started:

Sub WriteFormula()
Dim r As Range
Set r = Range("H1:H" & FindLastCell.Row)
r.Formula = "=YourFormula"
End Sub

Function FindLastCell() As Range
Dim LastColumn As Integer
Dim LastRow As Long
Dim LastCell As Range
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Set FindLastCell = Cells(LastRow, LastColumn)
Else
Set FindLastCell = Range("A1")
End If
End Function

Let me know if you need any more help.
--
Charles Chickering

"A good example is twice the value of good advice."


"Colin Foster" wrote:

> Hi,
>
> I have an Excel 2003 spreadsheet which retrieves the majority of its data
> from a Query; the spreadsheet then needs to perform a number of calculations
> on this data.
>
> If I select the "Fill Down Formulas in columns adjacent to data" property,
> then whilst the formulas fill the first time, when the query is refreshed
> then the formulas stop working - some have a #REF value & others start
> referencing, for example, Row 497 (which is where the previous "version" of
> the query's results extended to).
>
> So, two questions...
>
> First of all, am I doing "something wrong" with the query parameters, or is
> my error typical?
>
> Secondly, as an alternative, I was going to create a macro to copy the
> formulae down to the current last row, but I'm not sure of what code to use,
> so any help would be very welcome.
>
> Regards
> Colin Foster
> ps... Happy New Year!!
>

 
Reply With Quote
 
Colin Foster
Guest
Posts: n/a
 
      2nd Jan 2008
Hi Charles,

Yes... Excel can be SO frustrating when it tries to be helpful!!

I'll try both your Offset method (far easier as it avoids that dreaded
option... Operator Input!) and the macro tomorrow when I'm back in the office
& report back.

Thanks

"Charles Chickering" wrote:

> Colin, while I'm no expert on query's, this does sound normal to me. It sound
> like when the query updates it is moving the cells containing the resulting
> data. Excel attempts to be intelligent (and fails miserably) by tracking the
> cell that it was originally referenced to. While a macro would work, the
> "Offset" command might work for you. I learned this trick from Dick Kusleika
> at "Daily Dose of Excel"
> http://www.dailydoseofexcel.com/
>
> For your formula, instead of using the address of the cells in the query
> data, use an "Offset" of the cell it resides in. For example this would be a
> formula to sum columns "A" through "G" with the formula residing in column
> "H":
> =SUM(OFFSET(H1,0,-7):OFFSET(H1,0,-1))
> Thus the only "refereneced" cell is the cell that the formula itself resides
> in, the data can be moved, deleted etc. and the formula shouldn't break.
>
> If you want to write a macro anyhow, this should get you started:
>
> Sub WriteFormula()
> Dim r As Range
> Set r = Range("H1:H" & FindLastCell.Row)
> r.Formula = "=YourFormula"
> End Sub
>
> Function FindLastCell() As Range
> Dim LastColumn As Integer
> Dim LastRow As Long
> Dim LastCell As Range
> If WorksheetFunction.CountA(Cells) > 0 Then
> 'Search for any entry, by searching backwards by Rows.
> LastRow = Cells.Find(What:="*", After:=[A1], _
> SearchOrder:=xlByRows, _
> SearchDirection:=xlPrevious).Row
> 'Search for any entry, by searching backwards by Columns.
> LastColumn = Cells.Find(What:="*", After:=[A1], _
> SearchOrder:=xlByColumns, _
> SearchDirection:=xlPrevious).Column
> Set FindLastCell = Cells(LastRow, LastColumn)
> Else
> Set FindLastCell = Range("A1")
> End If
> End Function
>
> Let me know if you need any more help.
> --
> Charles Chickering
>
> "A good example is twice the value of good advice."
>
>
> "Colin Foster" wrote:
>
> > Hi,
> >
> > I have an Excel 2003 spreadsheet which retrieves the majority of its data
> > from a Query; the spreadsheet then needs to perform a number of calculations
> > on this data.
> >
> > If I select the "Fill Down Formulas in columns adjacent to data" property,
> > then whilst the formulas fill the first time, when the query is refreshed
> > then the formulas stop working - some have a #REF value & others start
> > referencing, for example, Row 497 (which is where the previous "version" of
> > the query's results extended to).
> >
> > So, two questions...
> >
> > First of all, am I doing "something wrong" with the query parameters, or is
> > my error typical?
> >
> > Secondly, as an alternative, I was going to create a macro to copy the
> > formulae down to the current last row, but I'm not sure of what code to use,
> > so any help would be very welcome.
> >
> > Regards
> > Colin Foster
> > ps... Happy New Year!!
> >

 
Reply With Quote
 
Colin Foster
Guest
Posts: n/a
 
      2nd Jan 2008
Dan,

Thanks for this... I'll give it a try tomorrow when I'm back in the office.
Cheers
Colin

"Dan R." wrote:

> Colin,
>
> Here's an example assuming your formula is in cell B1:
>
> Sub fillToLastRow()
> Dim lastRow As Long, fillRange As Range
>
> lastRow = Range("A65536").End(xlUp).Row
> Set fillRange = Range(Cells(1, 2), Cells(lastRow, 2))
>
> Range("B1").AutoFill fillRange
> End Sub
>
> --
> Dan
>
>
> On Jan 2, 11:53 am, Colin Foster
> <ColinFos...@discussions.microsoft.com> wrote:
> > Hi,
> >
> > I have an Excel 2003 spreadsheet which retrieves the majority of its data
> > from a Query; the spreadsheet then needs to perform a number of calculations
> > on this data.
> >
> > If I select the "Fill Down Formulas in columns adjacent to data" property,
> > then whilst the formulas fill the first time, when the query is refreshed
> > then the formulas stop working - some have a #REF value & others start
> > referencing, for example, Row 497 (which is where the previous "version" of
> > the query's results extended to).
> >
> > So, two questions...
> >
> > First of all, am I doing "something wrong" with the query parameters, or is
> > my error typical?
> >
> > Secondly, as an alternative, I was going to create a macro to copy the
> > formulae down to the current last row, but I'm not sure of what code to use,
> > so any help would be very welcome.
> >
> > Regards
> > Colin Foster
> > ps... Happy New Year!!

>

 
Reply With Quote
 
Colin Foster
Guest
Posts: n/a
 
      3rd Jan 2008
Dan, Charles,

First of all, further thanks for your advice & assistance. Managed to do
what I needed (I think!! Tested it & it seems to work... now to let the
operator loose!)

In the end, I had to go down the Macro route, rather than using the OFFSET
function as I have a fairly huge formula in one column & I received an error
message stating that the formula was too long!! Basically, it's checking
against a customer's code & depending on the result does a CEILING
calculation... some to th enearest 5p, others 9p.

Anyway, the final macro that I used was as follows...

Sub fillToLastRow()
Dim qrylastRow As Long, fillRange As Range, calclastRow As Long


qrylastRow = Range("A65536").End(xlUp).Row
calclastRow = Range("v65536").End(xlUp).Row

Range(Cells(4, 22), Cells(calclastRow, 29)).ClearContents
Range(Cells(4, 22), Cells(calclastRow, 29)).ClearFormats

Range("v3:Ac3").Copy _
Destination:=Range(Cells(4, 22), Cells(qrylastRow, 22))

End Sub

As you can see, I had to use a copy/paste option, rather than the autofill
as that didn't seem to work... however, using the logic provided enabled me
to reach the required result.

Also, having achieved this, then gave me the confidence to explore further
hence the lines which clear both contents & formats of the prior calculated
area before copying down the new one which helps keep the sheet looking tidy.

So, once again, many thanks for your collective troubles pointing the way...
and I've filed the code provided away for next time that I need it :-)

Regards
Colin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying formulae jamalhakem@gmail.com Microsoft Excel Worksheet Functions 0 23rd Oct 2008 01:17 PM
Copying formulae jamalhakem@gmail.com Microsoft Excel Worksheet Functions 4 22nd Oct 2008 03:34 PM
copying formulae roger finch Microsoft Excel Misc 2 11th Aug 2008 12:35 PM
Copying a formulae down a column that includes an INDIRECT =?Utf-8?B?QS5XZWJi?= Microsoft Excel Misc 10 14th Jan 2006 03:42 AM
copying down formulae katie Microsoft Excel Misc 1 24th May 2004 10:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:27 AM.