Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to copy formula from one cell to a range?

Hi,

I'm having a devil of a time copying a formula from one cell to a
variable range of cells using VBA. The code I came up with is the
following:

Dim ReferenceRow As Integer
Dim ReferenceCol As Integer

....

With Worksheets(ShtName)
.Range("A1").Copy
.Range(Cells(ReferenceRow, 5), Cells(ReferenceRow, _
ReferenceCol)).PasteSpecial Paste:=xlPasteFormulas
End With

It throws "run-time error 1004: Application-defined or object-defined
error" on the PasteSpecial command when I run it, and I can't figure out
why.

The issue is that I need the formula to copy over using relative
addressing (e.g., the cells referenced in the formula in A1 would need
to change according to the new cells), so I can't just copy and paste
the formula directly.

Any ideas?

Thanks...

....Jay
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default How to copy formula from one cell to a range?

Jason,
Worked OK for me but you will get your error message if the
values of ReferenceRow and/or ReferenceCol are invalid e.g 0.

HTH

"Jason Weiss" wrote:

Hi,

I'm having a devil of a time copying a formula from one cell to a
variable range of cells using VBA. The code I came up with is the
following:

Dim ReferenceRow As Integer
Dim ReferenceCol As Integer

....

With Worksheets(ShtName)
.Range("A1").Copy
.Range(Cells(ReferenceRow, 5), Cells(ReferenceRow, _
ReferenceCol)).PasteSpecial Paste:=xlPasteFormulas
End With

It throws "run-time error 1004: Application-defined or object-defined
error" on the PasteSpecial command when I run it, and I can't figure out
why.

The issue is that I need the formula to copy over using relative
addressing (e.g., the cells referenced in the formula in A1 would need
to change according to the new cells), so I can't just copy and paste
the formula directly.

Any ideas?

Thanks...

....Jay

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
RANGE EXCEL copy cell that meets criteria in a range confused Excel Worksheet Functions 3 March 27th 08 01:41 PM
copy range and paste into every 3rd cell of new range thomsonpa New Users to Excel 4 December 3rd 07 01:47 PM
range of Formula copy but not changing the content aw Excel Discussion (Misc queries) 4 November 20th 07 05:00 AM
I copy a formula and the results copy from the original cell brooklynsd Excel Discussion (Misc queries) 1 June 23rd 07 01:35 AM
Copy Excel Spreadsheet formula to a range via VB6 Kevin Excel Programming 4 October 4th 04 09:44 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"