+ Reply to Thread
Results 1 to 6 of 6

Pasting Issues Between Workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2014
    Location
    Whatcom, Washington
    MS-Off Ver
    2010
    Posts
    7

    Pasting Issues Between Workbooks

    Oy.

    New poster here, moderate VBA user. Begging for some help with a project that has had me stumped for the last 48 hours.

    What I am attempting to do:

    When closing a Workbook (named "CIR") I would like the data on the "DATA" tab to be cut and then pasted to another Workbook ("CIR Master") in the first blank row. Code posted below goes well until the last line.

    The Issue:

    Rather than pasting to the target Workbook ("CIR Master"), the code below pastes to the source workbook. I have tried many different ways of pasting, but no joy.

    Any help would be greatly appreciated.

    A second question...any ideas on what code could be used to see if the target Workbook is open? (ie, whether it would open as "read-only".)

    'Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim wbTarget As Workbook
    Dim wbThis As Workbook
    
    'Define Variables
    Set wbThis = ActiveWorkbook
    
    'Go to "DATA"
    Sheets("DATA").Select
    
    'Exit if "A2" is BLANK
    Range("A2").Select
    
    If IsEmpty(ActiveCell.Value) Then
        Exit Sub
    End If
    
    'Open CIR Master List
    Set wbTarget = Workbooks.Open("K:\CIR Master List.xlsm")
    
    'Clear Memory
    Application.CutCopyMode = False
    
    'Return to Source
    wbThis.Activate
    
    'Cut Rows
    Range("A62236").Select
    Selection.End(xlUp).Select
    intLastRow = ActiveCell.Row
    Range("$A$2:$S$" & intLastRow).Cut
    
    'Back to Target Workbook
    wbTarget.Activate
    
    'Find Next Empty Cell
    Range("A62236").Select
    Selection.End(xlUp).Select
    ActiveCell.Offset(1, 0).Select
    
    '...and it FAILS here
    ActiveSheet.Paste
    Last edited by OwlChutney; 06-19-2014 at 05:18 PM. Reason: Typo

  2. #2
    Registered User
    Join Date
    06-18-2014
    Location
    Whatcom, Washington
    MS-Off Ver
    2010
    Posts
    7

    Re: Pasting Issues Between Workbooks

    ...and I am fully aware that I have likely missed something obvious. Please be gentle?

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Pasting Issues Between Workbooks

    Does each workbook have only one sheet?

  4. #4
    Registered User
    Join Date
    06-18-2014
    Location
    Whatcom, Washington
    MS-Off Ver
    2010
    Posts
    7

    Re: Pasting Issues Between Workbooks

    The target workbook has just one worksheet. The source has multiple sheets, but the code takes me to the worksheet I wish to copy ("DATA") without problem.

    Thanks for the quick reply!

  5. #5
    Registered User
    Join Date
    06-18-2014
    Location
    Whatcom, Washington
    MS-Off Ver
    2010
    Posts
    7

    Re: Pasting Issues Between Workbooks

    Tried using the following to replace the final line of code (Activesheet.Paste):

    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    and

    With wbTarget
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=Ture
    End With
    Both give me a "Run-time error '1004': Application-defined or object-defined error"

    Looking into work arounds. Any thoughts?

  6. #6
    Registered User
    Join Date
    06-18-2014
    Location
    Whatcom, Washington
    MS-Off Ver
    2010
    Posts
    7

    Re: Pasting Issues Between Workbooks

    Solution has been found!

    The code below resides on the SOURCE spreadsheet:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim wbTarget As Workbook
    Dim wbThis As Workbook
    
    'Define Variables
    Set wbThis = ActiveWorkbook
    
    'Go to "DATA"
    Sheets("DATA").Select
    
    'Exit if "A2" is BLANK
    Range("A2").Select
    
    If IsEmpty(ActiveCell.Value) Then
        ActiveWorkbook.Close True
        Exit Sub
    End If
    
    If IsFileOpen("K:\FILENAME.xlsm") = True Then
        MsgBox "Master Data Spreadsheet is already open - Your input data has not been updated. Don't worry. Data will be updated next time you open the form.", vbExclamation, "CANNOT UPDATE MASTER"
        ActiveWorkbook.Close True
        Exit Sub
    Else
        Set wbTarget = Workbooks.Open("K:\FILENAME.xlsm")
    End If
    
    'Clear Memory
    Application.CutCopyMode = False
    
    'Return to Source
    wbThis.Activate
    
    'Cut Rows
    Range("A62236").Select
    Selection.End(xlUp).Select
    intLastRow = ActiveCell.Row
    Range("$A$2:$S$" & intLastRow).Cut
    
    'Back to Target Workbook
    wbTarget.Activate
    
    'Run Code "pastedata" from MasterList
    Application.Run ("CIRMasterList.xlsm!pastedata")
    
    End Sub
    This checks to see if the Target Workbook is open, copies from the Source Workbook...and then calls the following code - pastedata() - from the Target:

    Sub pastedata()
    
        'Find Next Empty Cell
        Range("A62236").Select
        Selection.End(xlUp).Select
        ActiveCell.Offset(1, 0).Select
    
        ActiveSheet.Paste
        
        ActiveWorkbook.Close True
        
    End Sub
    Hoping this may be able to help someone else out in the future.

    Peace.

+ 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. Copy and pasting issues with mouse connected
    By kas106 in forum Excel General
    Replies: 2
    Last Post: 04-11-2012, 04:52 AM
  2. Pasting issues
    By NH Dan in forum Excel General
    Replies: 4
    Last Post: 08-26-2011, 08:15 AM
  3. Issues with Pasting Cells
    By sarahwilson1000 in forum Excel General
    Replies: 0
    Last Post: 06-22-2011, 02:13 PM
  4. Various pasting issues
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-31-2011, 12:51 PM
  5. Pasting Personality issues
    By BBFB in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-16-2005, 01:05 PM

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