+ Reply to Thread
Results 1 to 3 of 3

Copy to another workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2014
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    58

    Copy to another workbook

    Hello,

    I use this code to upload data to another workbook on a server. But I need some help with adjustments.

    I do not want the data in target workbook to be replaced, I want to add new data the last available row in the target workbook.

    I would appreciate the help a lot !

    Sub CopyOpenItems()
       '
       ' CopyOpenItems Macro
       ' Copy open items to sheet.
       '
       ' Keyboard Shortcut: Ctrl+Shift+O
       '
       Dim wbTarget            As Workbook 'workbook where the data is to be pasted
       Dim wbThis              As Workbook 'workbook from where the data is to copied
       Dim strName             As String   'name of the source sheet/ target workbook
       Dim wbSource            As Worksheet
       
       Application.ScreenUpdating = False
          
        
       'set to the current active workbook (the source book)
       Set wbThis = ActiveWorkbook
        
       'get the active sheetname of the book
       strName = ActiveSheet.Name
        
       'open a workbook that has same name as the sheet name
       Set wbTarget = Workbooks.Open("P:\SE\AVK\C. Projektmodell\Caj Pärmen Utveckling\Databas.xlsx")
        
       'select cell A1 on the target book
       wbTarget.Sheets("Databas").Range("A1").Select
        
       'clear existing values form target book
       wbTarget.Sheets("Databas").Range("A1:L2000").ClearContents
    
       'activate the source book
       wbThis.Activate
        
       'clear any thing on clipboard to maximize available memory
       Application.CutCopyMode = False
        
       'copy the range from source book
       wbThis.Sheets("Databas").Range("A2:L2000").Copy
        
       'paste the data on the target book
       wbTarget.Sheets("Databas").Range("A1").PasteSpecial
        
       'clear any thing on clipboard to maximize available memory
       Application.CutCopyMode = False
        
       'save the target book
       wbTarget.Save
        
       'close the workbook
       wbTarget.Close
    
       'activate the source book again
       wbThis.Activate
        
       'clear memory
       Set wbTarget = Nothing
       Set wbThis = Nothing
       
       Application.ScreenUpdating = True
               
              
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Copy to another workbook

    You can replace your copy and paste lines with this:
     wbThis.Sheets("Databas").Range("A2:L2000").Copy wbTarget.Sheets("Databas").Range("A" & rows.count).end(xlup)(2)

  3. #3
    Registered User
    Join Date
    08-04-2014
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    58

    Re: Copy to another workbook

    Quote Originally Posted by StephenR View Post
    You can replace your copy and paste lines with this:
     wbThis.Sheets("Databas").Range("A2:L2000").Copy wbTarget.Sheets("Databas").Range("A" & rows.count).end(xlup)(2)
    That works really great!!

    Is it also possible to match range A in the sourcebook with range A in the targetbook? if the data already exist in the target workbook
    I want the code to skip that row and move on to the next.

+ 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 data one workbook to another workbook without opening workbook
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2014, 11:28 AM
  2. [SOLVED] Copy Values From Each Workbook in Folder to a Single Sheet in New Workbook +Workbook names
    By Arsham24 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2012, 07:42 PM
  3. [SOLVED] copy active sheet, create new workbook, name new workbook based on cell reference
    By jm0392 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2012, 07:12 PM
  4. Replies: 1
    Last Post: 04-01-2006, 03:50 PM

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