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!
Last edited by CBron12; 07-15-2010 at 03:04 PM.
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
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
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?
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
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
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
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Bingo! The only thing I had to do was remove the .Offset(0,1) from the destination DstRng. Thank you so much!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks