VBA: Copy > Look > Paste

mhroberts

New Member
Joined
Oct 13, 2010
Messages
23
As a bit of a background, let me tell you I'm a Google-programmer. That is not to say I work for Google, but instead that I use Google as my manual for programming. So, in other words, please be gentle!

Now, here's my problem, and I feel like I've done a LOT of searching.

I have 3 values on a sheet I want to copy and paste into a table on another sheet, but the row is a variable based off a row-header, which can be identified based on yet another cell.

Let me walk you through what I'm trying to do:

  1. On Sheet "Post", copy cell H11 as 'gNew', copy cell H12 as 'rNew', and copy cell H13 as 'nNew'
  2. On Sheet "Post", look at cell C3
  3. On Sheet "Roster", find the row that has a match in Column "A" to "Post!C3"
  4. On Sheet "Roster", paste 'gNew' into that found row, Column "I"
  5. On Sheet "Roster", paste 'rNew' into that found row, Column "J"
  6. On Sheet "Roster", paste 'nNew' into that found row, Column "K"

End.

Thanks ahead of time for your awesome time and help!
[/LIST]
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I think this will do what you want:
Code:
Sub MyCopyMacro()
'
    Dim myValue As String
    
'   Copy value from Post sheet
    myValue = Sheets("Post").Range("C3")
    
'   Find value on Roster sheet
    Sheets("Roster").Activate
    On Error GoTo err_Check
    Cells.Find(What:=myValue, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    On Error GoTo 0
 
'   Paste values
    Cells(ActiveCell.Row, "I").Value = Sheets("Post").Range("H11")
    Cells(ActiveCell.Row, "J").Value = Sheets("Post").Range("H12")
    Cells(ActiveCell.Row, "K").Value = Sheets("Post").Range("H13")
    Exit Sub
    
'   Error checking to handle if value is not found
err_Check:
    If Err.Number = 91 Then
        MsgBox "The value of " & Chr(34) & myValue & Chr(34) & " is not found on the Roster sheet"
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
 
End Sub
 
Upvote 0
This seems like it should work, but I'm getting an error that says it can't find the value on the Roster sheet, even though that's where the original value is pulling from.
 
Upvote 0
Are the values EXACTLY the same, no extra spaces at the end of the word or anything like that?
 
Upvote 0
Yes, exactly the same. A Validation List is built using a Dynamic Named Range, that list is available to pick from, built from the Roster sheet, placed on the sheet "Import" and then Cell C3 on "Post" is an IFERROR statement which looks at that cell on "Import."

I changed your code to refer directly to the cell on "Import", thinking the IFERROR statement may be tripping it up, but I get the exact same error.
 
Upvote 0
Can you post the code, exactly as you have it now, with the modifications you made?
 
Upvote 0
Code:
'
    Dim myValue As String
    
'   Copy value from Post sheet
    myValue = Sheets("Import").Range("B3")
    
'   Find value on Roster sheet
    Sheets("Roster").Activate
    On Error GoTo err_Check
    Cells.Find(What:=myValue, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    On Error GoTo 0
 
'   Paste values
    Cells(ActiveCell.Row, "I").Value = Sheets("Post").Range("H11")
    Cells(ActiveCell.Row, "J").Value = Sheets("Post").Range("H12")
    Cells(ActiveCell.Row, "K").Value = Sheets("Post").Range("H13")
    Exit Sub
    
'   Error checking to handle if value is not found
err_Check:
    If Err.Number = 91 Then
        MsgBox "The value of " & Chr(34) & myValue & Chr(34) & " is not found on the Roster sheet"
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If

Pretty much exactly the code as you had it. :)
 
Upvote 0
Note that you code now has references to three different sheets: "Post", "Roster", and "Import". Are you sure all the references are correct for the task at hand?

Right now, it is capturing the value of cell B3 on the "Import" sheet, and then looking for it on the "Roster" sheet.

If it finds it, it is copying the values from cells H11, H12, and H13 from the "Post" sheet into columns I,J, and K on the "Roster" sheet.

Is that your intention?
 
Upvote 0
Yes. That's exactly correct.

If it helps, the cell it should be looking FOR is within a named range called "Table_Members" but keep in mind it's a Dynamically named range. My previous attempts at this were fruitless partly because VBA didn't like named ranges with formulas or something.
 
Upvote 0
OK, let's check a simple example here.

What value is currently showing in cell B3 on the "Import" sheet?
What cell does that currently match on the "Roster" sheet?
 
Upvote 0

Forum statistics

Threads
1,203,094
Messages
6,053,507
Members
444,667
Latest member
KWR21

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top