+ Reply to Thread
Results 1 to 3 of 3

Making one Macro work for any range of cells instead of creating several

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2013
    Location
    Fort Worth, TX
    MS-Off Ver
    Excel 2010
    Posts
    1

    Making one Macro work for any range of cells instead of creating several

    Hello,

    I have been given the task of editing an existing macro, with close to no experience with macros or VBA.

    What it is suppose to do is select and copy a certain range of cells and paste them to another range of cells. What I am suppose to figure out is, instead of making 30 of the same macros for 30 different cell ranges, how can the code be manipulated to work on any range of cells without me having to create several of the same macros. Below is the code I am trying to fix.

    Thank you!


    Sub Move1_1()
    '
    ' Move1_1 Macro
    '

    '
    Dim CR, PR As Range
    Set CR = Range("Comp1_1")
    Set PR = Range("Team1_1")

    CR.Copy

    PR.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Making one Macro work for any range of cells instead of creating several

    you could specify the range in a cell let's say: cell(1,1)-> sheet1!a4:aa4
    and the receiving range in cell(1,2) -> sheet2!a1:aa1

    so cr=range(cells(1,1))
    and pr=range(cells(1,2))

    and so on...

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Making one Macro work for any range of cells instead of creating several

    This prompts for the source and destination ranges to copy\paste.

    Sub Mover()
        Dim rngSource, rngDest
        On Error GoTo Done  'User Canceled
        Set rngSource = Application.InputBox("Select range to copy. ", "Source Range", Type:=8)
        Set rngDest = Application.InputBox("Select range to paste to. ", "Destination Range", Type:=8)
        On Error GoTo 0
        rngSource.Copy
        rngDest(1).PasteSpecial xlPasteValues, xlNone, False, False
        Application.CutCopyMode = True
    Done:
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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