+ Reply to Thread
Results 1 to 3 of 3

Help With Copy and Paste Macro

  1. #1
    Registered User
    Join Date
    07-18-2013
    Location
    Worcester
    MS-Off Ver
    Microsoft 365
    Posts
    19

    Help With Copy and Paste Macro

    Hi,

    I am trying to create a macro that copies and pastes all the data from one tab in a closed workbook into an open and active one. Two main considerations: the name of the source file changes every day and the amount of data with vary each day (some days one line of data, other times 10,000+).

    I have set up an 'Inputs' tab so that I can manually adjust the source file name each day, with a button linked to the macro to run it. I have used this for other macros and has worked fine. This is what I have so far:

    Sub PullClosedData()
    Dim filePath_1 As String
    Dim SourceWb_1 As Workbook
    Dim TargetWb As Workbook

    Set TargetWb = ActiveWorkbook
    i = 5 'row number of first input sheet in "Inputs" tab

    'Tab 16: Data - NonTask

    filePath_1 = TargetWb.Sheets("Inputs").Range("B" & i).Value

    Set SourceWb_1 = Workbooks.Open(filePath_1)

    'Transfer values from x to y:
    TargetWb.Sheets("Data - Non Task").Range("A1").Value = SourceWb_1.Sheets("Data - Non Task").Range("A1")
    With SourceWb_1.Sheets("Data - Non Task").UsedRange

    'Now, paste to y worksheet:
    TargetWb.Sheets("Data - Non Task").Range("A1").Resize( _
    .Rows.Count, .Columns.Count) = .Value

    End With

    SourceWb_1.Close

    End Sub


    I keep getting a 'Subscript out of range' error but no option to debug, so I have no idea where I'm going wrong. The source workbook opens so at least I'm getting that far.

    I have absolutely no idea what I am doing as I'm very new to this. Please be kind!

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Help With Copy and Paste Macro

    Quote Originally Posted by welshraz View Post
    I keep getting a 'Subscript out of range' error but no option to debug, so I have no idea where I'm going wrong.
    When you're in the code, use F8 to step through it slowly and watch which line it fails on. Without a file attached it's more difficult to debug it for you.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Help With Copy and Paste Macro

    It sounds like an issue with a worksheet that does not exist... are you 100% sure both workbooks have a worksheet with the EXACT name "Data - Non Task"?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 01-13-2016, 08:50 PM
  2. Copy paste - cute paste macro almost working
    By graiggoriz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2014, 12:33 PM
  3. Copy & paste in macro doesn't paste all cells
    By pltrapper in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-13-2013, 09:22 AM
  4. [SOLVED] Simple copy and paste macro- Paste special help needed.
    By hernancrespo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2012, 07:02 AM
  5. [SOLVED] Macro to Find, copy, paste, find, copy, paste, find, copy, paste, u get the picture....
    By westsoldman in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2012, 01:12 AM
  6. 2 Macro's: only vertical copy/paste action and copy-paste 14 columns to the right.
    By vdongen in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2010, 10:34 AM
  7. Copy and Paste macro needs to paste to a changing cell reference
    By loulou in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2005, 07:06 AM

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.6.0 RC 1