Data from 2 workbooks into another..

cfer

Well-known Member
Joined
Jul 29, 2002
Messages
560
Hi,

I have a spreadsheet, that needs data from 2 other Workbooks.

i would like is a Macro that would be able to do the following.

The file that I want to have the Data saved in would be called:

15062008

Using this file name, look up that number plus add the following
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Did not quite finish question, hit the enter key.


I want to be able to get th data from 2 files and enter into another file.

I have a file named 15062008.

I want to be able to use this file name, and add _Core.

So I end up with 15062008_Core.

Open up this file, copy all the Sheet called "Misc" to the File 15062008, Sheet2 called "Data".

I then want to be able to do the same with the next file, but take 7 of the file name,

ie 08062008_Core

Copy all the Sheet called "Misc" to the top row of the file 15062008, Sheet2, called "Data".

The original file is using a date, so next week I would save the file as 22062008, then be able to open 22062008_Core, and 15062008_Core retrieve the info.

The week after saved as 29062008 so on and so on.

Any help appreciatted.

Cfer
 
Upvote 0
here is some code I use to connect to other closed excel worksheets, maybe it will help

Sub Inport_Data()

Dim lstrFileName As String
lstrFileName = Application.GetOpenFilename(FileFilter:="All Files(*.*), *.*", Title:="Select the File to Import")

GetDataFromClosedWorkbook lstrFileName, "A1:C21", Worksheets("Sheet3").Cells(1, 1), True
End Sub


Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String, _
TargetRange As Range, IncludeFieldNames As Boolean)
' requires a reference to the Microsoft ActiveX Data Objects library
' if SourceRange is a range reference:
' this will return data from the first worksheet in SourceFile
' if SourceRange is a defined name reference:
' this will return data from any worksheet in SourceFile
' SourceRange must include the range headers
'
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' open the database connection
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' close the database connection
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file or source range is invalid!", _
vbExclamation, "Get data from closed workbook"
End Sub
 
Upvote 0
Hi wilgry

Thanks for the reply.

The files and data are in Excel.

I tried them in Excel, but get error messsage.
Thanks again.
 
Upvote 0
Thanks wilgry,

you probably right, I'll try to explain what I am after.

I am using excel only.

What I want to achieve is..

1). have a file called FAT_15062008

Have 2 sheets, one called "Week_1" the other "Week_2"

have a macro that will open a excel file called 15062008_Core.

Note, if possible have it so I can use the numbers that correspond with the 2 files. 15062008.

As these are dates that I use to keep track of files each week.

Copy all the Sheet called "Misc" from the file 15062008_Core to the Sheet called "Week_1" in the file called FAT_15062008. sheet "Week_2".

2)

I woud then like to open another file called 08062008_Core and dothe same, copy the sheet "Misc" to FAT_150662008.

The Macro would need to subtract 7 from the 15 part of file called FAT_15062008 each time for the second part to work.

Each week a new file is made adding 7 to the last week, so next week it would be 22062008, I would need to subtract 7 from 22062008.

That way each time I run the vmacro it will look for the previous week file, when the 2nd part is to run.

Hope this makes it easy as to what i am afer.

cfer
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

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