Dynamic worksheet name in Index/Match formula - excel

Background
The U.S. government releases a daily report I use for work. Recently, the government changed how they release their report. Instead of releasing the report as a text file, they are now releasing it as pdf. Unfortunately, this has affected how I pull information from the report. Previously, I pulled the information from the site as it was saved as a text file. Now, I download the pdf and convert it to an excel file.
I have 1 workbook that contains saved copies of the report on individual worksheets, e.g. April 30, April 29, April 28, etc. Unfortunately, due to the conversion, the information on each page can vary by a row or two, e.g. Information_A on April 30 worksheet could be on row 30 and on April 29 it could be on row 33.
On the first page of the workbook, I have a summary page which pulls key information from the previous page, e.g. Summary would pull information from the April 30 worksheet.
Problem
The problem now comes from how I pull the information from the excel file. Since the information is not on the same row, I thought the Index/Match method would be best. The formula I made is below. The reference name, Information_A, is in column A and the value I need from Information_A is in column P.
=(INDEX('April 30'!P:P,MATCH("Information_A",'April 30'!A:A,0)))
Since my worksheets constantly change I thought I would be able to pull the last worksheet name and then input it into the formula like so,
=(INDEX('[previous_sheet]'P:P,MATCH("Information_A",'[previous_sheet]'!A:A,0)))
So I tried to figure out how to pull the previous sheet name. I tried a few different ways and ended up using ChrisB's solution here.
I had saved the formula he provided (below) as PrevSheet_2
=IF( MATCH(wsName,wsNamesArray,0)-1 =0, ERROR.TYPE(7), INDEX(wsNamesArray,MATCH(wsName,wsNamesArray,0)+1))
Where, wsNamesArray and wsNameare as follows,
wsNamesArray: =RIGHT(GET.WORKBOOK(1),LEN(GET.WORKBOOK(1))-FIND("]",GET.WORKBOOK(1))) & T(NOW())
wsName: =MID(CELL("filename", INDIRECT("A1")),FIND("]",CELL("filename",INDIRECT("A1")))+1,255) & T(NOW())
Which resulted in the 'final' formula of,
=(INDEX("'"&PrevSheet_2&"'!P:P",MATCH(Information_A","'"&PrevSheet_2&"'!A:A",0)))
which doesn't work. This formula gives me a #VALUE! error.
I tested the formula without using PrevSheet_2 and typed in the worksheet name, e.g. April 30, and got the correct value, so I know that part is working. I also test PrevSheet_2 in a cell and got the correct previous sheet as well, so I know the problem lies in the combination of the 2.
My question, then, is how to make this
=(INDEX("'"&PrevSheet_2&"'!P:P",MATCH(Information_A","'"&PrevSheet_2&"'!A:A",0)))
formula work.
I have a feeling PrevSheet_2 is in the wrong data type however I don't know how to fix it.
EDIT:
I have also tried using INDIRECT (below) and got a #REF! error.
=INDEX(INDIRECT("''"&PrevSheet_2 &"'!P:P"),MATCH("Information_A",INDIRECT("'"&PrevSheet_2&"'!A:A"),0))

Refer this link .. Define SheetNames array (in Formulas > Define Name) as SheetNames = MID(GET.WORKBOOK(1),FIND("]",GET.WORKBOOK(1),1)+1,LEN(GET.WORKBOOK(1))) .. Then in excel formula you can get to previous sheet name as INDEX(SheetNames,SHEET()-1)
You can also define PreviousSheetName = INDEX(MID(GET.WORKBOOK(1),FIND("]",GET.WORKBOOK(1),1)+1,LEN(GET.WORKBOOK(1))),SHEET()-1)
You can put that in INDIRECT function as suggested by #BigBen
So Formula will be =INDEX(INDIRECT("'"&PreviousSheetName &"'!P:P"),MATCH("Information_A",INDIRECT("'"&PreviousSheetName&"'!A:A"),0))
See .. Sheet function gives us Index of the current sheet. So Sheet()-1 gives us Index of the previous sheet
After my answer I noted you have put single quotes TWICE in your indirect formula. Hence, the #REF error

Related

Dealing with Circular References in Excel when there is no apparent circular reference

Forgive me if this isn't the right place, but I have seen a couple of posts on here relating to excel troubles like this and they don't really help. I am creating a tournament handicap tracker for my snooker league, which involves a main sheet with every week's handicap, as well as individual week sheets for tracking the scores between players every week.
Here is the basic layout of the overview page. (I've removed names for privacy purposes)
Then, each week has its own page that tracks scores and calculates handicaps.
The trouble is the references between each week and the handicaps stored on the main overview page. For example, the "previous handicap" for week 1 is hard coded, but for every other week, it depends on the value in the previous week on the main page. I.e, the "Previous Handicap" for Week 2 is a copy of the "Week 1" column on the main page.
To do this, I have used the following formula
=IFERROR(VLOOKUP([#Player],TblPlayerOverview,MID(CELL("filename"), FIND("]",CELL("filename"))+6,500)+2), "N/A"), where TblPlayerOverview is the main table.
This initial calculation works great, but as soon as I try and calculate it for Week 3, I get a circular reference error, and the week 2 previous handicap column seems to iterate everytime I try and calculate week 3. This leads me to believe there is somehow a circular dependence between week 2 and the main page, but I honestly cannot find it and I have looked for quite some time. I would appreciate some help.
The calculation for copying over the weeks new handicaps to the main page goes as follows: =VLOOKUP([#Player],INDIRECT("'"&INDEX(TblPlayerOverview[#Headers], COLUMN())&"'"&"!A17:D32"),4).
I currently only have three week sheets as I deleted the newer ones to try and simplify the problem.
I understand that this may be difficult to comprehend just from the screenshots, so I have included a link to my google drive in which the excel file has been uploaded.
I will appreciate any and all help as I am truly lost.
Thanks,
Ben
SnookerLeagueResults.xlsx
Edited/Updated Answer following comments
After discussing with OP in the comments to this answer and using a simplified file, the issue is because there is no cell reference value when CELL("filename") is used. Reading the documentation for the formula, there is an optional argument to specify which cell you want information about and that "if omitted, the information specified... is returned for the cell selected at the time of calculation."
This argument was not included in OP's formula so it would evaluate the filename based on the current cell that is selected. This means that when the previous handicap formula on the Week 3 sheet was selected and calculated, anywhere that CELL("filename") was used would now refer to the active cell on the Week 3 sheet. But as CELL("filename") is also on the Week 2 sheet, this formula would be referring to the Week 3 sheet and so be a circular reference.
This means the solution is to specify the cell reference argument whenever CELL("filename") is used so that the cell it refers to is constant and doesn't change when the selected cell changes. What is used in the argument doesn't really matter, using cell A1 of each sheet is probably easiest. So the new formula for the original file should be:
=IFERROR(VLOOKUP([#Player],TblPlayerOverview,MID(CELL("filename",A1), FIND("]",CELL("filename",A1))+6,500)+2,FALSE), "N/A")
Sidenote, I've also added a 'FALSE' argument to the end of the VLOOKUP formula to ensure it retrieves an exact match, not an approximate one.
Original Answer
The formula in B18:B32 on the 'Week 3' sheet refers to the 'TblPlayerOverview' table on the 'Player Overview' sheet. But column F in this table refers to the 'A17:D32' range on the 'Week 3' sheet (and so is implicitly looking at the B18:B32 range). This is your circular reference.
I think the way to overcome this would be to change the table reference to the previous week/sheet, so Week 3 refers to the previous handicap in the Week 2 table rather than the 'Player Overview' sheet.

Index function to pull value from a closed workbook, but use cell value for sheet name

I have 2 workbooks and within each book, each sheet are the months of the year. The source book and target book have identical sheet names, eg. Aug 2019, Sep 2019..... I have created the following formula in the target book, cell B9, to pull a value from the source workbook, cell AJ46:
=INDEX('C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]Aug 2019'!$AJ$46,1)
It works fine, even with the source book closed.
In the target book I have loaded cell Q3 with the following formula :
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
This loads cell Q3 in the target book with the current active sheet. So cell Q3's value is Aug 2019.
There are 20 copies of the INDEX formula in the target book, and the only thing that changes is the source cell at the end, which in the formula above is AJ46.
To prevent constant changing of the INDEX formula when I extend the sheets to future months, I would like to use the value in cell Q3 in place of entering the actual sheet name.
I have tried :
=INDEX('C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]"&Q3&"'!$AJ$46,1)
But it produces an error.
I am using Excel 2007.
Firstly, if you only want to return a specific cell, say AJ46, why won't you just reference the cell address directly rather than using an INDEX function? I am sure the following will also return the desired value regardless if the other workbook is open or not.
='C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]Aug 2019'!$AJ$46
Secondly, there have been ample discussions on this topic online and even in this community as in this post. The simple conclusion is:
there is no way to make a dynamic workbook/worksheet/range/cell name using excel formula and return the desired value from a closed workbook.
Thirdly, it can be done using VBA. If it is an option for you, you may want to add #vba to your tags so other contributors may be able to help you as #vba it not my expertise. Someone in the post I mentioned before provided a vba code that works for direct cell reference but not incorporating INDEX. If that's an option for you you may want to give it a go.
Fourthly, for your information it can also be done using PowerQuery but it is only available in Excel 2010 and later versions. Besides, PowerQuery worked best with large raw data-set rather than structured report anyway.

Indirect Function with Sheet Names

I have a list of all of the sheet names in my workbook on Sheet1 and I am trying, without success, to use the INDIRECT function to reference a cell on another sheet using said name. I am aware that this is a common question, I have looked at about 10 other questions/answers on this website in addition to others. For some reason, those solutions are not working and I am kinda losing my mind.
The sheet names are all in column A
They are named after dates (format mm-dd-yy) <- I'm assuming this is the problem?
The cell that I am trying to reference is always in O1 on every sheet in the workbook.
I have tried:
=INDIRECT(A1&"!O1")
=INDIRECT("'"A1"'&!O1")
and a few other amalgamations. Again, I have looked into so many other solutions at this point I am just left with asking for help with my situation, which is apparently unique (or more likely it is some blaring user error). Please help me before immediately removing the post.
If I have this right, the names of your sheets are dates, and you have the names listed as dates in Column A. So, they will appear as dates, but are actually Excel's background number for a date (days since 1/1/1900).
If you use the Formulas tab in the ribbon to Evaluate Formula, you should see the date pop in to your formula as a 5 digit number.
If you are not doing anything else with these dates, you could try selecting column A and formatting as text. This solved the issue in my test setup. This will still work if you are using the date for other functions, but you will have to tell Excel it's a date using DateValue()
After formatting Column A as text, I used =INDIRECT("'"&A1&"'!O1")
Try,
=indirect(text(a1, "\'mm-dd-yy\'\!\O\1"))

Trying to find the last non-empty cell in a specific row/range over multiple sheets

I looked at a couple suggestions here and didn't find something that specifically worked for me. I would prefer not to use an Array Formula if possible. However, if that's the only way, I'll try to work with it.
I am working on an attendance workbook of sorts in Excel 2013. The workbook contains a Calculations sheet, a Lists sheet and a sheet for each month of the year. The Lists sheet has a list for names and a list for the months. I have included a link to a sample workbook to demonstrate how the sheets are setup and the basic functions, names, lists, etc that I am using.
My sample workbook:
https://app.box.com/s/mgvums1vmnmnec7e7d0p4vi5daoyo5nv
When looking for the last non-empty cell in a specific row/range, I receive the correct result when I use the following LOOKUP on each specific Month sheet: (Which I found after some digging on a few sites) =LOOKUP(2,1/(C5:AG5<>""),C$4:AG$4)
- For the Month of Feb, the last entry in the range C5:AG5 is in AA5, which is the 25th.
I also receive the correct result when I use the following Lookup from any non-Month sheet: =LOOKUP(2,1/(Feb!C5:AG5<>""),Feb!C$4:AG$4)
That being said, the two specific issues I am running into, are as follows:
I have been unable to work out how to come up with the last non-empty cell (which is the last marked day of attendance) when using Name and Month information selected in the Combo Box/Form Controls. Simply said, in my sample workbook, I want to know the last day in Feb that Johnny was in attendance.
I've tried starting simple, just using the "Month" I selected. I tried a few different itterations of the following:
=LOOKUP(2,1/(Month&"!C5:AG5<>"""),Month&"!C$4:AG$4")
The closest I've gotten was the above Lookup, which ends up turning into what you see below (when viewed in Calculation steps). It of course returns a #VALUE! error.
=LOOKUP(2,1/"Feb!C5:AG5<>""",Month&"!C$4:AG$4")
I also tried this Array Formula and it gave me a result of 4. Which, isn't the proper result. And, I don't know where the result actually came from.
=MAX((INDIRECT(F4&"!"&"C5:AG5")<>"")*(ROW(INDIRECT(F4&"!"&"C$4:AG$4"))))
I have been unable to work out how to come up with the last non-empty cell across all the Month sheets when using the selected Name from the Combo Box/Form Control. Basically, I want to know the last day of the year that Johnny was in attendance. In this sample workbook, that would fall on April 11th. I understand that the formula would just return "11". I believe I can format the result after the fact, using the resulting sheet name. (In this sample case, Apr)
I am fairly certain I will need to use a VLOOKUP. I just can't seem to break the barrier between the general concepts of what I want and the proper way of formulating it.
Any help would be appreciated. I'm feeling pretty lost at the moment. If you need more info from me, please let me know.
Thanks,
Kurewe
The key is to use INDIRECT with the exact same LOOKUP formula you already had and use the MATCH formula to determine the row number for the selected name.
Place the following formulas:
G3: =MATCH(F3,NameList,0)+4 (find the item number of the selected name within NameList)
H2: =F4&"!C4:AG4" (construct the address for the date row for the selected month)
H3: =F4&"!C"&G3&":AG"&G3 (construct the address to use for the selected name and month)
E9: =INDEX(MonthList,ROW()-ROW(E$8)) copy down to E20 (find the name of the month for this row using MonthList)
G9: =E9&"!C4:AG4" copy down to G20 (construct the address for the date row for the month in E9)
H9: =E9&"!C"&G$3&":AG"&G$3 copy down to H20 (construct the address to use for the selected name and the month in E9)
F9: =IFERROR(LOOKUP(2,1/(INDIRECT(H9)<>""),INDIRECT(G9)),"(n/a)") copy down to F20 (find the last day with an "X" in it using the addresses in G9 & H9)
G4: =LOOKUP(2,1/ISNUMBER(F9:F20),ROW(F9:F20)-ROW(F8)) (find the index of the first cell with "(n/a)" in it)
F5: =LOOKUP(2,1/(INDIRECT(H3)<>""),INDIRECT(H2)) (find the last day with an "X" in it for the addresses in H2 & H3)
F6: =INDEX(MonthList,G4)&"-"&INDEX(F9:F20,G4) (construct the date of the last day of attendance within the year)

EXCEL: Counting worksheets dynamically using INDIRECT

I've come across several discussions on using INDIRECT and 3D references, however none seem to address (or fix) this problem specifically.
I'm trying to count the position of a worksheet without using VBA. Each sheet represents a month, and has a standardized name (e.g. JAN-15, FEB-15, MAR-15, etc.). My idea is to find the position by counting the number of sheets between JAN-15 and the current sheet. A1 of each sheet = 1, which gets summed across the sheets using a 3D reference.
Using February as an example, I can hard code this with no problem:
=SUM('JAN-15:FEB-15'!A1)
When I do this, I get the expected result of 2. However, I want each month's sheet to dynamically generate its position without having to change the formula each time. So I tried this:
Cell named FIRSTMONTH ='JAN-15
Cell named CURRMONTH =RIGHT(CELL("FILENAME",A1),6)
N1 =CONCATENATE("'",FIRSTMONTH,":",CURRMONTH,"'!A1")
(N1 evaluates correctly as 'JAN-15:FEB-15'!A1)
When I try this formula to generate the position:
=SUM(INDIRECT(N1))
I get a #REF! error. I've also tried this variation:
=SUM(INDIRECT("N1"))
Which returns 0.
Anyone have an idea of how I can get this to work? Or is there a better way that doesn't use VBA?
-David
Assuming you won't have any gaps perhaps try counting the number of month between the first month and the current one, e.g.
=DATEDIF(FIRSTMONTH,CURRMONTH,"m")+1

Resources