Copy data from one workbook that is on specified location and paste it to another workbook

aman2059

Board Regular
Joined
Jan 17, 2016
Messages
75
Hi,

I am trying to copy data from one workbook that is on specific location and then paste data to the another workbook and then save the 'another' workbook with specific name.

I am able to save workbook but I am unable to copy data from one workbook to another. code gives an error. Below is my code. Please help.

Code:
Sub Save214012016()
 
    Dim FName As String
    Dim FPath As String
    Dim NewBook As Workbook
    Dim wb2 As Workbook
    
 
    FPath = "C:\Users\aman.bhardwaj\Desktop\Ideas\Learning process\Master files\testing\saved report"
    FName = "LearnersinMandateDataExport_3657_" & Format(Date, "mmddyyyy") & ".xlsx"
 
    Set wb2 = Workbooks.Open("C:\Users\aman.bhardwaj\Desktop\Ideas\Learning process\30th November 2015\raw\LearnersinMandateDataExport_3657.xls")
    ThisWorkbook.Activate
    Set NewBook = Workbooks.Add
    Windows("LearnersinMandateDataExport_3657.xls").Activate
    Sheets("Sheet1").Select
    wb2.Sheets("Sheet1").Copy Before:=NewBook.Sheets(1)
 
    If Dir(FPath & "\" & FName) <> "" Then
        MsgBox "File " & FPath & "\" & FName & " already exists"
    Else
        NewBook.SaveAs Filename:=FPath & "\" & FName
    End If
    
     
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This is resolved. :) Below is the code

Code:
Sub Save6()
    
    Dim FName As String
    Dim FPath As String
    Dim NewBook As Workbook
    Dim wb2 As Workbook
    
    FPath = "C:\Users\aman.bhardwaj\Desktop\Ideas\Learning process\Master files\testing\saved report"
    FName = "LearnersinMandateDataExport_3657_" & Format(Date, "mmddyyyy") & ".xlsx"
    
    Set wb2 = Workbooks.Open("C:\Users\aman.bhardwaj\Desktop\Ideas\Learning process\30th November 2015\raw\LearnersinMandateDataExport_3657.xls")
    wb2.Sheets("Sheet1").Copy    'Creates a new workbook with the copied sheet
    Set NewBook = ActiveWorkbook
    
    If Dir(FPath & "\" & FName) <> "" Then
        MsgBox "File " & FPath & "\" & FName & " already exists"
    Else
        NewBook.SaveAs Filename:=FPath & "\" & FName
    End If
         
End Sub
 
Upvote 0
Bumping to this thread.

I am able to copy all the data in new workbook with above code. However, the problem is that in the old workbook, data has few filters in it. So when it copies data to new workbook, it also copies filters. Could anyone please suggest me the edit to above code to only copy visible cell to the new workbook..

Thank you in advance. :)
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top