Code To Look For Part Then Find Earliest And Latest Years

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have a file with a list of part numbers in column A on sheet 2. I need the code to look for these numbers on sheet 1 in column AC. When the number is found I need it to look at the earliest start year in column O where it appears and the latest end year in column Q then put in column B next to it on sheet 2.

Using just one part number as an example the very earliest year it appears on sheet 1 is 1974 and the very latest is 1990 so the result next to it would be 74-90 in column B then it would move onto the next number and so on...

Thanks

Excel Workbook
MNOPQACAD
2061983051987AXRAD60010
3061983111990AXRAD60010
4101990011993NYA
5051987081990AXRAD60010
6061990101994NYA
7091980031986NYA
8061986091989NYA
9091987011993NYA
10061986101987NYA
11101987011993NYA
12012007122009NYA
13011985101987NYA
14051974011978AXRAD60010
15081977041984AXRAD60010
16091978041984AXRAD60010
17
18
19
20Earliest year so takes 74
21
22Latest year so takes 90
23
Sheet1




Excel Workbook
ABC
1PartYear
2AXRAD6001074-90Result from sheet 1
3
Sheet2
 
Thanks rallcorn but I get a runtime error '9' subscript out of range and when I debug it points to Sheets("Sheet2").Activate
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Do you have a sheet in the workbook named Sheet2? What are the actual names of the worksheets in your file?
 
Upvote 0
Thanks but this returns 0-0 in each cell in column B?
My error. There should be a period in front of Columns as shown below.
Code:
sd = WorksheetFunction.Min(.Columns("O").SpecialCells(xlCellTypeVisible))
ed = WorksheetFunction.Max(.Columns("Q").SpecialCells(xlCellTypeVisible))
 
Upvote 0
In the code I just posted in #9, the variable in the part number in column A & "i" row of loop. Because the Min & Max formulas are array formulas with an "if" statement restricting the results to the Sheet1 Col AC rows that match the Sheet2 A&"i" part variable, it is simply finding the MIN year shown for that part number and the MAX year shown for that part number and parsing the right 2 digits from both of these. Another formula concatenates the Min & Max pieces into Sheet2 Col B.

I meant with the formula you first posted in #2
 
Upvote 0
I meant with the formula you first posted in #2

Same thing as I described:
Because it is an array formula, the "If" statements are restricting the rows on Sheet1 Col AC to those rows that match the single Sheet2 Col A part for which the formula is being "applied" and then finding the minimum in Sheet1 Col O for ONLY those restricted rows and doing the same thing with the maximum in Sheet1 Col Q for ONLY those restricted rows.

Also, please note: Since you later stated that you're planning to run macro from your Personal Macro Workbook against numerous other workbooks:
If you choose to use the macro I gave you, you'll need to change it up. Get rid of
Code:
Dim wbThis As Workbook: Set wbThis = ThisWorkbook
wbThis.Activate
and then you'll have to add some code to refer to the workbook you're running it against AND a way to "find" the right sheets (this would also pertain to the references to Sheet1! within the formulas).

Good luck!
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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