+ Reply to Thread
Results 1 to 2 of 2

VBA Code for Copy and Paste Exact Formulas in Set Ranges

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    VBA Code for Copy and Paste Exact Formulas in Set Ranges

    Hello,

    I am trying to put together a simple tool to copy and paste exact formulas using vba input boxes. I often have to copy exact formulas from various ranges and this little tool, when set in my toolbar, will help a lot.

    The main problem I'm having is that the "paste range" is offset based on where the "copy range" is located within a worksheet. Here is my code:

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub CopyCells()

    Dim rngFirst As Range
    Dim rngSecond As Range
    Dim cell As Range

    'Get copy range
    On Error GoTo Cancelled
    Set rngFirst = Application.InputBox("Please select area to copy", "Obtain Range Object", Type:=8)
    Err.Clear
    On Error GoTo 0

    'Get paste range
    On Error GoTo Cancelled
    Set rngSecond = Application.InputBox("Please select area to paste", "Obtain Range Destination", Type:=8)
    Err.Clear
    On Error GoTo 0

    'Set paste range using copy range
    For Each cell In rngFirst
    rngSecond(cell.Row, cell.Column).Formula = cell.Formula
    Next cell

    Cancelled:
    End Sub
    ---------------------------------------------------------------------------------------------------------------------------

    I believe the problem lies entirely within my loop. Any help you can provide would be greatly appreciated.

    Thank you for your time!
    Chris

  2. #2
    Registered User
    Join Date
    02-11-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA Code for Copy and Paste Exact Formulas in Set Ranges

    The loop was my entire problem. I fixed it be using this instead:

    With Application.ActiveSheet
    rngSecond.Formula = rngFirst.Formula
    End With

    Simple, clean and this tiny macro now helps wonders in my toolbar.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 05-03-2013, 07:19 AM
  2. exact copy and paste
    By jgross in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2013, 03:11 PM
  3. Replies: 2
    Last Post: 11-15-2011, 11:15 AM
  4. Copy and paste and keep EXACT formatting
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2008, 03:58 PM
  5. exact copy/paste
    By jmwismer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-20-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1