+ Reply to Thread
Results 1 to 2 of 2

Macro to save copied data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-11-2013
    Location
    london
    MS-Off Ver
    Excel 365
    Posts
    270

    Macro to save copied data

    Hi,

    The below macro basically copies a work sheet, opens a work book, pastes the copied data, closes the work sheet it just copied and then saves and closed the work book, then it reopens and repeats.

    When it saves and closes and reopens the workbook it takes a long time.

    Is there a way for it to only save and close the workbook once all the open work sheets to copied and pasted are completed?

    Sub Mega_Dump()
    
    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       'target workbook
    Dim cn As String
    Dim shName As String
    Dim wasOPEN As Boolean
    
        
    shName = Range("D6").Value
        
    Set wbThis = ActiveWorkbook                                     'set to the current active workbook (the source book)
       
    On Error Resume Next
    Set wbTarget = Workbooks("My FILE PATH")           'try to find already open workbook
    
    If Not wbTarget Is Nothing Then
        wasOPEN = True                                              'make a note that it was already open
    Else                                                            'open it if it was closed, put your path in the space noted
        Set wbTarget = Workbooks.Open("My FILE PATH")
    End If
          
    wbTarget.Sheets(shName).Range("xfd1").EntireColumn.ClearContents  'select cell A1 on the target book
    wbThis.Activate                                                 'activate the source book
    Application.CutCopyMode = False                                 'clear any thing on clipboard to maximize available memory
      
    wbThis.ActiveSheet.Range("b2:af44").Copy                         'copy the range from source book
                                                                    'paste the data on the target book ( for just values PasteSpecial xlValues )
    wbTarget.Sheets(shName).Cells(2, Columns.Count).End(xlToLeft).Offset(, 6).PasteSpecial xlPasteAll
     
    Application.CutCopyMode = False                                 'clear any thing on clipboard to maximize available memory
    
    If wasOPEN Then wbTarget.Save Else wbTarget.Close True          'save the target book, close it if it was closed before
     
    wbThis.Close                                              'activate or close the source book again
    
    Set wbTarget = Nothing                                          'clear memory
    Set wbThis = Nothing
    
       Application.OnTime Now + TimeValue("00:00:01"), "Mega_Dump"
    End Sub

  2. #2
    Registered User
    Join Date
    12-05-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    13

    Re: Macro to save copied data

    Following code will open a folder to pick where all your workbooks (To Copy Data From ) are saved and then will copy them to the current workbook.

    
    Sub FoldPick()
        Dim FDiag As FileDialog
        Dim Paths
        Dim FName
        Dim WB As Workbook
        Dim LastRow
        Dim ThisWBLAstRow
        Set FDiag = Application.FileDialog(msoFileDialogFolderPicker)
        FDiag.AllowMultiSelect = False
        FDiag.Title = "Select the Folder which has the Files"
        FDiag.Show
        Paths = FDiag.SelectedItems(1)
        FName = Dir(Paths & "\*.csv")
        With ThisWorkbook.Worksheets(2)
            .Range("A1") = "Date"
            .Range("B1") = "Employee Name"
            .Range("C1") = "Login Time"
            .Range("D1") = "Logout Time"
            .Range("E1") = "Total System Time"
        End With
        Application.DisplayAlerts = False
        Do Until Len(FName) = 0
            Workbooks.Open (Paths & "\" & FName)
            Set WB = Workbooks(FName)
            With WB
                LastRow = .Worksheets(1).Range("A65536").End(xlUp).Row
                ThisWBLAstRow = ThisWorkbook.Worksheets(2).Range("A65536").End(xlUp).Row
                .Worksheets(1).Range("A2:E" & LastRow).Copy
                ThisWorkbook.Worksheets(2).Range("A" & ThisWBLAstRow + 1).PasteSpecial xlPasteValues
                .Close No
            End With
            FName = Dir
        Loop
       
        ThisWorkbook.Close Yes
        Application.DisplayAlerts = True
    End Sub
    Let me know if it helps.
    Cheers

+ 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. [SOLVED] When data is copied to another workbook, hyperlinks as not copied or they don't work
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-17-2014, 06:46 AM
  2. [SOLVED] Macro will not move copied data to next blank row?
    By DG370 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-15-2013, 10:54 AM
  3. Macro - insert line based on copied data
    By epedersen in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-03-2012, 03:21 PM
  4. wrong data copied into new cell from macro
    By johnmerlino in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2011, 10:42 AM
  5. macro to copy data where the ID is equal to what is copied from another cell
    By blink359 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-19-2010, 07:27 AM

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