Current location: Hot Scripts Forums » Programming Languages » Visual Basic » Copy Excel Formula using VBA


Copy Excel Formula using VBA

Reply
  #1 (permalink)  
Old 09-25-06, 05:40 PM
rich8008 rich8008 is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Copy Excel Formula using VBA

How do you copy Excel Formulas using VBA so that they maintain there relative ranges. For example, I want to copy the Formula

=SUM(A1:A10) from cell A11 to cell D31 so that it is =SUM(D2130)

If I use something like Cell(4,31).Formula = Cell(1,11).Formula then I get the =SUM(A1:A10) in D31 which is not what I am trying to do.

To date I have always just summed it in VBA or something like that instead but I really can't avoid it anymore as the end user needs to be able to adjust values after the fact and not have to re-run the Macro.

I am struggling though creating the formulas using Address property of the new range but it is rather difficult and would require a re-write for almost any change.

Any help on this would be greatly appreciated. I have looked through my books, Help, internet and this forum and I can't seem to find anything.
Reply With Quote
  #2 (permalink)  
Old 09-26-06, 08:22 PM
rich8008 rich8008 is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Post Found a way...

I used the following to copy the formula maintaining its relative reference. This seems pretty simple now, I don't know why I couldn't figure it out.

wks.Cells(i + 13, 7).FormulaR1C1 = wks.Cells(i, 3).FormulaR1C1
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - Copy string and paste as formula stitches43188 Visual Basic 0 07-12-06 10:16 AM
Importing And EXporting An Excel Dilna ASP.NET 2 05-02-06 04:49 PM
EXCEL VBA: Delete all empty worksheets crmpicco Visual Basic 1 05-16-05 07:53 PM
How to load Excel data in VB-textbox or label? Nellie Visual Basic 1 03-15-05 06:44 PM
Long sales copy versus short sales copy. newsguy General Advertisements 0 11-19-04 07:05 AM


All times are GMT -5. The time now is 01:17 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.