Consulting

Results 1 to 3 of 3

Thread: Help with Copy and paste

  1. #1
    VBAX Regular
    Joined
    Mar 2013
    Posts
    45
    Location

    Help with Copy and paste

    im using the following VBA

    Sub CopyAndPaste()
    Dim sh As Worksheet, S As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
        If InStr(sh.Name, "Union dues") > 0 Then
            Set S = sh
            Exit For
        End If
    Next sh
    With Sheets("Blank")
        .Range("AE11:AR11").Copy Destination:=S.Range("G4:T4")
        Application.CutCopyMode = False
    End With
    
    End Sub
    But its erroring out here
    .Range("AE11:AR11").Copy Destination:=S.Range("G4:T4")

    Im trying to copy a Range of cells from my work sheet entitled BLANK and paste that range of cells to a work sheet that Contains Union dues with a wildcard at the end...

    any thoughts?

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    You haven't SET S so excel doesn't know where to put it!, in your SET you effectively set WORKSHEET to equal WORKSHEET rather than sh.name
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you may try:

    [vba]
    Sub CopyAndPaste1()
    'Worksheet("Union dues") exists
    Worksheets("Blank").Range("AE11:AR11").Copy
    Worksheets("Union dues").Range("G4:T4").Paste
    End Sub

    'or

    Sub CopyAndPaste1()
    'Worksheet("Union dues") exists
    Worksheets("Union dues").Range("G4:T4").Value = Worksheets("Blank").Range("AE11:AR11").Value
    End Sub
    [/vba]



    [vba]Sub CopyAndPaste2()
    'Worksheet("Union dues") does not exist or not known
    On Error Resume Next
    Worksheets("Union dues").Range("G4:T4") = Worksheets("Blank").Range("AE11:AR11")
    If Err.Number <> 0 Then MsgBox Err.Number & vbCrLf & Err.Description
    On Error GoTo 0
    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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