Macro to open workbook and paste data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have written code to open a file and copy the data from sheet1 and paste in on sheet "Imported Data"

I need the code amended so that I do not need to delete sheets2


Your assistance is most appreciated


Code:
 Sub Open_Workbook()
ChDir ("C:\downloads")
Sheets(2).Delete

Dim nb As Workbook, tw As Workbook, ts As Worksheet, TheFile As String, ThePath As String
ThePath = "C:\extract\"
TheFile = Dir(ThePath & "PTAW*Sales" & "*.csv")



'If TheFile = False Or IsEmpty(TheFile) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(TheFile)
nb.Sheets(1).Copy After:=Workbooks("PTAW BR1 Sales.xlsm").Sheets(1)
 Sheets(2).Name = "Imported Data"
 ChDir ("C:\my Documents")
 
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I did try that earlier. However, when the data is copied to the destination workbook, it inserts a new sheets

I want to copy the data from sheet1 on the source darta workbook and paste this on sheet "Imported Data"

Kindly amend my code accordingly
 
Upvote 0
Have you looked at creating a connection between the two workbooks using Power Query?
 
Upvote 0
I started working with it a bit ago and it saves you from writing a lot of VBA. The name makes it sound daunting but it is actually fairly easy to use. You just go into the destination workbook, click on the data tab and go through the wizard to extract the data from the workbook that has the source data. Once thing is that the source data (I believe) must be in a table format.
 
Upvote 0
it is very easy but how does it update when source data changes ?
 
Upvote 0
Hi howard, you click the Refresh All button on the data tab
1644560289160.png
 
Upvote 0
Thanks. I imported the Data using Power Query (on Data Tab , I selected Get Data from File> From Text/CSV) . However If I change the data in the source file and select Refresh All the data does not change in the destination workbook
 
Upvote 0
Can you click on the "Queries and Connections" button and then hover over the query to see if there are any error messages?
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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