+ Reply to Thread
Results 1 to 3 of 3

Open workbook, copy rows, paste to end of current workbook

Hybrid View

gmn734 Open workbook, copy rows,... 07-30-2013, 09:53 AM
Naveed Raza Re: Open workbook, copy rows,... 08-25-2013, 05:07 AM
HaHoBe Re: Open workbook, copy rows,... 08-25-2013, 05:16 AM
  1. #1
    Registered User
    Join Date
    06-18-2013
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    8

    Open workbook, copy rows, paste to end of current workbook

    Hi,

    I am trying to put together some code to do the following
    1. Open OLDDATA workbook (using the dialog box, file name and path will be different every time)
    2. Copy all rows except row1 from OLDDATA, olddatasheet (this is the only worksheet in the file) to the workbook that the macro was run from (workbook=NEWDATA, sheet=newdatasheet. this will always be called the same). newdatasheet will already have data so i need it to copy to the next available row
    3. Close OLDDATA workbook without saving

    Can anyone help? my attempt is below...

    ' copy_new_forecasts Macro
    '
    'open OLDDATA
    Application.Dialogs(xlDialogOpen).Show
    
    Dim wbTarget As Workbook 'workbook where the data is to be pasted (NEWDATA, newdatasheet)
    Dim wbThis As Workbook 'workbook from where the data is to be copied (OLDDATA, olddatasheet)
    Dim strName As String 'name of the source sheet/ target workbook (olddatasheet)
    'set to the current active workbook (OLDDATA)
    Set wbThis = ActiveWorkbook
    
    'get the active sheetname of the book (olddatasheet)
    strName = ActiveSheet.Name
    
    'set target workbook
    Set wbTarget = Workbooks("NEWDATA").Sheets("newdatasheet")
    
    'activate the OLDDATA book
    wbThis.Activate
    
    'copy the range from OLDDATA(all rows except row1, number of rows with data will be varied each time)
    wbThis.Range("???????").Copy
    
    'paste the data on the next available row of NEWDATA, newdatasheet
    wbTarget.Range("A1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    'close the OLDDATA workbook
    wbThis.Close

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Open workbook, copy rows, paste to end of current workbook

    try this code.......
    Sub copying()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    mynew = ActiveWorkbook.Name
    Dim nsh As Worksheet, FR As Long, osh As Worksheet, LR As Long
    check = Application.Dialogs(xlDialogOpen).Show
    If check <> True Then
    Exit Sub
    End If
    myold = ActiveWorkbook.Name
    Set osh = Workbooks("" & myold & "").Sheets("olddatasheet")
    With osh
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    Set nsh = Workbooks("" & mynew & "").Sheets("newdatasheet")
    With nsh
        FR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
        osh.Range("A2:A" & LR).EntireRow.Copy
        .Range("A" & FR).PasteSpecial (xlPasteValues)
        Application.CutCopyMode = False
        Workbooks("" & myold & "").Close False
        .Range("A1").Select
    End With
    MsgBox "Done !"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    or else find the two attached file for validating..

    Hope this will work
    Attached Files Attached Files
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open workbook, copy rows, paste to end of current workbook

    Hi, gmn734,

    you have dimmed wbTarget as Workbook so you may only assign a workbook to that variable, not a workseet as well:
    ' copy_new_forecasts Macro
    '
    'open OLDDATA
    Application.Dialogs(xlDialogOpen).Show
    
    Dim wbTarget As Workbook 'workbook where the data is to be pasted (NEWDATA, newdatasheet)
    Dim wbThis As Workbook 'workbook from where the data is to be copied (OLDDATA, olddatasheet)
    Dim strName As String 'name of the source sheet/ target workbook (olddatasheet)
    'set to the current active workbook (OLDDATA)
    Set wbThis = ActiveWorkbook
    
    'get the active sheetname of the book (olddatasheet)
    strName = ActiveSheet.Name
    
    'set target workbook
    Set wbTarget = Workbooks("NEWDATA")
    
    ''activate the OLDDATA book
    'wbThis.Activate
    
    'copy the range from OLDDATA(all rows except row1, number of rows with data will be varied each time)
    wbThis.Sheets(strName).Range("A1").CurrentRegion.Offset(1, 0).Copy
    
    'paste the data on the next available row of NEWDATA, newdatasheet
    wbTarget.Sheets("newdatasheet").Range("A1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    'close the OLDDATA workbook
    wbThis.Close
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. Simple Open Workbook, Copy Sheet, Paste In New Workbook Question
    By alulla in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 02:10 PM
  2. Replies: 0
    Last Post: 05-08-2013, 02:47 PM
  3. Open workbook, filter values, copy/paste into Active workbook.
    By niceguy21 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2013, 12:17 PM
  4. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  5. Simple task: Open a workbook, copy a range, paste into another workbook
    By fbonetti in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2011, 01:12 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