Match a date to pick up data from another workbook

girapas

Board Regular
Joined
Apr 20, 2004
Messages
150
That's correct:
I've a book per month (say JAN2012, FEB2012 etc.). Each book has sheets, one for every workday (say day1, day2, day3 etc.) where I put data as follows:

book: JAN2012


Book1
ABCD
1date:2/1/2012
2X1X2X3
3AAA329628999803
4BBB753419913895
day1



Book1
ABCD
1date:3/1/2012
2X1X2X3
3AAA654134032126
4BBB599618958919
day2



Book1
ABCD
1date:4/1/2012
2X1X2X3
3AAA575447291230
4BBB538847986289
day3


In another book (book2) I've a sheet named 'database':

book2


Book1
ABCDEFG
1----AAA--------BBB----
2dateX1X2X3X1X2X3
32/1/2012
43/1/2012
54/1/2012
65/1/2012
79/1/2012
810/1/2012
911/1/2012
1012/1/2012
11etc.
database


How to run a macro in book2, to get the next date from sheet 'database', column A, look up for that in the book with the same month-year (JAN2012, FEB2012 etc.), in sheet with the same date, pick up the values for AAA/X1,X2,X3 and BBB/X1,X2,X3 and put them in corresponding cells of 'database'?
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Why not put all the data on one sheet, with a column for date? That would make Excel very happy.
 
Upvote 0
Because every workday data are put from a user in a specific form, and then he send me by email an Excel file. Periodically I update the database.
 
Upvote 0
I think this is what you want. Try it out.

Run this code from the current month Workbook, with the current day (or last day) sheet as the active sheet. It should look at book 2 column A to find the date of the active sheet as entered in cell B1 and then get the next date from column A of the database sheet.

Code:
Sub glrapas()
Dim sh As Worksheet, sh2 As Worksheet, wb2 As Workbook
Set sh = ThisWorkbook.ActiveSheet
Set wb2 = Workbooks("Book2") 'Edit Workbook name
Set sh2 = wb2.Sheets("database") 'Edit Sheet name
Set dt = sh2.Range("A:A").Find(sh.Range("B2").Value, LookIn:=xlValues)
If Not dt Is Nothing Then
Set nSh = ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
nSh.Range("B1") = dt.Offset(1, 0).Value
End If
End Sub
Code:
 
Upvote 0
Thanks for the reply. When run this line of the code: Set wb2=Workbooks("Book2.xlsx") I get a runtime error - subscript out of range.
Also, I think that in line Set dt = sh2.Range("A:A").Find(sh.Range("B2").Value, LookIn:=xlValues) "B2" must be "B1".
 
Upvote 0
Thanks for the reply. When run this line of the code: Set wb2=Workbooks("Book2.xlsx") I get a runtime error - subscript out of range.
Also, I think that in line Set dt = sh2.Range("A:A").Find(sh.Range("B2").Value, LookIn:=xlValues) "B2" must be "B1".

Yep, the B2 is a typo, it should be B1. Ther workbook Book2 has to be open when you run that macro. I did not include a workbooks.Open line in the code. You can add that in if you know how. As long as it is in the same directory as your Month workbook, you won't have to worry about the path, etc. Just add a line before the Find statement:

Workbooks.Open "Book2.xlsx" .

Then I think I would add before the End Sub:

Workbooks("Book2").Close False
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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