+ Reply to Thread
Results 1 to 6 of 6

Thread: Copy and Paste to location based on cell value

  1. #1
    Registered User
    Join Date
    07-15-2010
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question Copy and Paste to location based on cell value

    Hello,

    I'm trying to copy a range of data from one location and paste it to another location, with the paste location based on a cell reference. For instance, in the attached workbook I've input "a" into cell C4 on the "Control" sheet. What I need is a macro that will copy and paste the data in cells C6:C11 on the "Control" sheet below the corresponding "a" in the "Output" sheet.

    Thanks in advance for the help!
    Attached Files Attached Files
    Last edited by CBron12; 07-15-2010 at 03:04 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Copy and Paste to location based on cell value

    Hello CBron12,

    Welcome tot he Forum!

    I have added a button and attached the macro below to it. This has all been added to the attached worksheet.
    Sub CopyAndPaste() 
    
      Dim DstCol As String
      Dim DstRng As Range
      Dim SrcRng As Range
      
        Set SrcRng = Worksheets("Control").Range("C6:C11")
        
        DstCol = Worksheets("Control").Range("C4").Value
        
        Set DstRng = Worksheets("Output").Cells(4, DstCol).Offset(0, 1)
    
        SrcRng.Copy
        DstRng.PasteSpecial Paste:=xlPasteAll
        
        Application.CutCopyMode = False
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-15-2010
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Copy and Paste to location based on cell value

    I think that is really close to what I need, however I may have simplified my example a little too much. The spreadsheet I am working on uses stock symbols in the place of the letters and I get a mismatch error when I run the macro (see attached). This is probably a pretty easy fix right?
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Copy and Paste to location based on cell value

    Hello CBron12,

    Here is the new macro code. I haved this to the attached workbook.
    Sub CopyAndPaste()
    
      Dim DstCol As Variant
      Dim DstRng As Range
      Dim SrcRng As Range
      Dim Symbol As String
      
        Set SrcRng = Worksheets("Control").Range("C6:C11")
        
        Symbol = Worksheets("Control").Range("C4").Value
        
        On Error Resume Next
          DstCol = WorksheetFunction.Match(Symbol, Worksheets("Output").Range("3:3"), 0)
          If Err <> 0 Then
             MsgBox "Stock symbol '" & Symbol & " not found."
             Exit Sub
          End If
        On Error GoTo 0
        
        Set DstRng = Worksheets("Output").Cells(4, DstCol).Offset(0, 1)
    
        SrcRng.Copy
        DstRng.PasteSpecial Paste:=xlPasteAll
        
        Application.CutCopyMode = False
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Copy and Paste to location based on cell value

    Hello Cbron12,

    I just noticed that I didn't remove the Offset statement. Here is the corrected code and workbook.
    Sub CopyAndPaste()
    
      Dim DstCol As Variant
      Dim DstRng As Range
      Dim SrcRng As Range
      Dim Symbol As String
      
        Set SrcRng = Worksheets("Control").Range("C6:C11")
        
        Symbol = Worksheets("Control").Range("C4").Value
        
        On Error Resume Next
          DstCol = WorksheetFunction.Match(Symbol, Worksheets("Output").Range("3:3"), 0)
          If Err <> 0 Then
             MsgBox "Stock symbol '" & Symbol & " not found."
             Exit Sub
          End If
        On Error GoTo 0
        
        Set DstRng = Worksheets("Output").Cells(4, DstCol)
    
        SrcRng.Copy
        DstRng.PasteSpecial Paste:=xlPasteAll
        
        Application.CutCopyMode = False
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    07-15-2010
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Copy and Paste to location based on cell value

    Bingo! The only thing I had to do was remove the .Offset(0,1) from the destination DstRng. Thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0