+ Reply to Thread
Results 1 to 4 of 4

Copying from one Workbook to another not working

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2010
    Posts
    43

    Wink Copying from one Workbook to another not working

    Hi

    I'm trying to copy a range of data from one workbook, open another workbook, check that no one else already has it open and if not paste the data at the end of the existing data. I then would like to save & close the file and delete a few sheets from the original workbook.

    When I step through the macro it opens the file then jumps back to the start of the macro without pasting or closing the newly opened workbook.

    Any suggestions would be greatly appreciated.

    Workbooks.Open ("G:\COMMERCIAL CONTRACTS\Templates_UsefulStuff\Commercial Job Progress.xlsx")
    
    If ActiveWorkbook.ReadOnly Then
    ActiveWorkbook.Close SaveChanges:=False
    MsgBox "Another user is in the Commercial Job Progress file. Once they have exited retry."
    GoTo ProcedureDone
    Else
    ThisWorkbook.Sheets("CommercialQuotes").Range("A2:I2").Copy Destination:=ActiveWorkbook.Sheets("DATA").Range("A" & Rows.Count).End(xlUp).Offset(1)
    ActiveWorkbook.Close SaveChanges:=True
    
    ThisWorkbook.Sheets("Input").Select
    ThisWorkbook.Sheets("CommercialQuotes").Delete
    ThisWorkbook.Sheets("Tags").Delete
    ThisWorkbook.Sheets("CSV").Delete
    End If
        
        
    ProcedureDone:
        Exit Sub
    Thanks Jo

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Copying from one Workbook to another not working

    Hi Jo,

    Untested but see how this goes:

    Dim wbThisWorkbook As Workbook
        Dim wbDestinationWorkbook As Workbook
        Dim varMyTab As Variant
    
        Set wbThisWorkbook = ThisWorkbook
        Set wbDestinationWorkbook = Workbooks.Open("G:\COMMERCIAL CONTRACTS\Templates_UsefulStuff\Commercial Job Progress.xlsx")
    
        If wbDestinationWorkbook.ReadOnly Then
            wbDestinationWorkbook.Close SaveChanges:=False
            Set wbThisWorkbook = Nothing
            Set wbDestinationWorkbook = Nothing
            MsgBox "Another user is in the Commercial Job Progress file. Once they have exited retry."
            GoTo ProcedureDone
        Else
            wbThisWorkbook.Sheets("CommercialQuotes").Range("A2:I2").Copy Destination:=wbDestinationWorkbook.Sheets("DATA").Range("A" & Rows.Count).End(xlUp).Offset(1)
            wbDestinationWorkbook.Close SaveChanges:=True
            Set wbDestinationWorkbook = Nothing
            'The following will delete each tab from the array in the 'wbDestinationWorkbook'
            For Each varMyTab In Array("CommercialQuotes", "Tags", "CSV")
                wbThisWorkbook.Sheets(CStr(varMyTab)).Delete
            Next varMyTab
            wbThisWorkbook.Sheets("Input").Select
            Set wbThisWorkbook = Nothing
        End If
        
    ProcedureDone:
        Exit Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    06-26-2014
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2010
    Posts
    43

    Re: Copying from one Workbook to another not working

    Thanks so much Robert, that worked a treat! The only change I made was to swap the Destination method to the Paste method so that I could paste special values as there were a couple of formulas in the copied range.
    Cheers
    Jo

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Copying from one Workbook to another not working

    Thanks so much Robert, that worked a treat!
    You're welcome. If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert

+ 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. Copying VBA from one workbook to another not working
    By ksp2014 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2014, 03:35 PM
  2. [SOLVED] Copying Multiple Ranges to another workbook not working
    By Excel987 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-08-2013, 11:10 PM
  3. [SOLVED] Copying to separate workbook marco not working
    By Will_iam in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 04-30-2013, 12:11 PM
  4. [SOLVED] Copying Filtered Data From 2 Sheets of One Workbook to Another Isn't Working Correctly
    By ScotyB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2012, 06:30 PM
  5. Replies: 0
    Last Post: 06-29-2011, 04:53 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