Dynamically specify worksheet without indirect() in Excel - excel

In my database, I have created separate worksheets for different sources of data, to make the database more structured for the users. In the summary page I created, I would like to dynamically refer to these worksheets, to easily visualize the data.
The "sheet selection" looks as follows.
Site should determine from which sheet the data comes. Afdeling and Data are columns in these sheets, and its values are used to find the data range (row start and row stop).
In this excellent question and answer, it is explained how to dynamically refer to a specific data range by Function, without the use of Indirect() (Why I don't want indirect). However, it does not answer how to refer to a sheet.
Is it possible to select the worksheet in an excel file by function, without using Indirect()?

Related

Excel chart adapting to table with formula-generated rows -- without named ranges?

I have table-like data, and I'm looking to make a chart that displays that data. Since the data is that formula-generated from other data in the workbook, I don't know in advance how many lines it will have. I want to make a chart that adapts to this data.
Up to this part of the question, I could use named ranges to solve this.
The thing with the solution with named ranges is that it does not scale well when I have many of these charts. I have a Python script that generates CSV files that I import into my workbook as a sheet, and I don't want to have to know in advance how many of them there will be, or what they will be named. I only want to be able to import the CSV files into a new or existing sheet, and copy-paste the formulas from another working sheet, as well as be able to replace the existing CSV data with new data.
With named ranges, I would have to manually create a named range for each series of each sheet, and I would have to use the sheet's name so that the named range can be visible to the whole workbook (in order to analyze the data in a global sheet) without any name conflict. This is (more or less) acceptable when I do these steps myself now, but if I want to redo this in a few months, or if I pass my workbook to someone else, we wouldn't know why it's not working with a new sheet.
So is there any way to get this done without delving into VBA stuff? I'm using a recent version of Excel.
Per the comments, try using pivot tables, making your range extend far beyond expected results and filtering out "blanks" in your pivot and generate your chart from that. The default pivot chart is ugly but you can remove buttons and format as needed. Just remember to refresh your pivot during every update period.

Excel - Change source of chart data depending on text in cell

I have one excel file in which I have multiple sheets with financial statements from different companies (called Databas.xlsx). The structures of these sheets are identical. Then I have another excel file that I wish to use to analyse these financial statements using charts. Thus, I must get data from the different sheets into my analysis file. Doing this from one sheet is no problem, as I can simply create a chart and mark the data I need from this sheet, so that the chart data range would be something like this:
=[Databas.xlsx]Kopparbergs!$C$3:$K$3
where "Kopparbergs" is the sheet name in Databas.xlsx. The problem I am facing is that I want to be able to change the sheet name that is put into this formula by writing the name in a cell (because that would enable me to change multiple charts at once). So just to clarify, in the formula written above, I want to be able to change the word "Kopparbergs" by writing text in a cell. If that is not possible, how would I accomplish this? That is, how do you create a chart that can change its content depending on a text in a cell that corresponds to a sheet?
So rather than using Indirect I think you need to use two named ranges for referencing when using a Chart.
This previous answer looks like a good guide to implement (not sure about etiquette of just copy & pasting previous answers so I'll just provide the link):
Dynamic chart range using INDIRECT: That function is not valid (despite range highlighted)

Reference another Excel Worksheet via a Keyword

I am not sure if this is too ambitious. Currently I'm generating a bunch of pivot tables through our server (I won't go into detail) but our main stocktaking spreadsheet references these generated pivot tables weekly and pulls their figures through.
i.e using code ='C:\Users\ITS1\Documents\Test\Establishment\190303[Stocktake.xlsx]Data4'!$D$3 which is simple enough - just referencing whatever figure is in that cell on the pivot table.
There is a particular bunch of figures on some pivot tables each week called 'Redemption' which, depending on what other kind of sales an establishment generates, shift to different cells each week. So I can't reference them like other figures which are always fixed to the same cells on every generated pivot table.
Is there a way I can reference the pivot table by the keyword 'Redemption' and take the figure from the cell directly next to it which is the actual figure we need to record?
While this isn't hugely urgent, it would be quite handy. These figures can just be manually entered but just trying out some automation to eliminate my human error :)
If you don't want to have to open the external workbook, this approach might be feasible:
In the external workbook, create a helper sheet that uses a formula to look up the Redemption field value. This could possibly be done with a GetPivotData formula. The exact formula depends on your data and without seeing that I can't write it.
The idea here is that the helper sheet and the location of the cell on the helper sheet does not change. When the pivot table is refreshed, the formula calculates the result.
In the other workbook you can now use an absolute reference to that helper sheet and cell and don't need to open the external file to retrieve the value.

Column header validation with different sheets using macros

In Excel, I have 4 column headers (Id, Name, Age, Place).
I have 3 Excel worksheets with same column headers and different data.
I want to make a consolidated worksheet and the constraint is I have to check whether the column header is the same in all 3 worksheets and move it into the consolidated worksheet.
In the 1st worksheet I have values in the first row.
In 2nd worksheet, the first row is empty and the second row has data.
In 3rd worksheet, the first and second rows are empty and the 3rd row has data.
The 4th worksheet is the consolidated one. I want all 3 worksheet's data in this sheet.
How to do it with macros in vba?
There's built-in functionality for data consolidation.
You'll need to move your data so it's all on the same row but in the end will probably be much easier than writing code from scratch.
How to consolidate
Follow these steps to consolidate several worksheets into a master worksheet:
If you haven't already, set up the data in each constituent sheet by doing the following:
Ensure that each range of data is in list format. Each column must have a label (header) in the first row and contain similar data. There must be no blank rows or columns anywhere in the list.
Put each range on a separate worksheet, but don't enter anything in the master worksheet where you plan to consolidate the data. Excel will do this for you.
Ensure that each range has the same layout.
In the master worksheet, click the upper-left cell of the area where you want the consolidated data to appear.
Note: To avoid overwriting existing data in the master worksheet, ensure that you leave enough cells to the right and below this cell for the consolidated data.
Click Data → Consolidate (in the Data Tools group).
In the Function box, click the summary function that you want Excel to use to consolidate the data. The default function is SUM.
Here is an example in which three worksheet ranges have been chosen:
Select your data.
More information and the remaining instructions are at the source.
If you want to automate the process, record a macro as you performs the necessary steps and then you can edit as required.
See: Recording a Macro to Generate Code as well as Revising Recorded Visual Basic Macros. If you're not familiar with Excel (or whatever you're planning on automating with VBA), do that before attempting VBA.
This VBA tutorial is highly recommended: Excel VBA For Beginners and Microsoft's Documentation.

Point to a relative other Sheet in Excel

I'm trying to find a way to from a Cell get the data from a cell in the Sheet that lies to the Left (down in the tray) of the current Sheet.
I know how to call to other sheets via
=Sheet1!A1
But now I need something best explained with
=Sheet[-1]!A1
Any ideas?
Using the tab order as a fundamental part of your calculations is a complicated and risky approach to Excel calculations. Excel offers many alternatives which you'd be better off using:
A simplified version of belisarius's suggestion is: =INDIRECT(A1 & "!A2") where cell A1 has the name of your datasource sheet and A2 has the name of your target cell in your datasource sheet. If you know the name of your sheet of interest (or can look it up in some way), use this method.
If you need to do this often, you might want to export the data into an actual database (i.e. MS Access). Then you can make standard SQL queries and import the results into your Excel file.
If you absolutely want to go the VBA route, then you'd have to write some code that:
3a. Grabs all the names of the active workbook and stores them in an array.
3b. Identifies the index number of the currently active workbook in that array. Subtract 1 from that index to get the sheet to the left.
3c. Gets the cell value from that sheet.
You can also get freaky with Named Ranges. In Excel 2003, go to Insert->Name->Define, add a new Named Range and you can use that name in your calculations instead of referring to the cell by row and column.
Edit
The whole Idea with this one, is that
you have the Sheets arranged, and are
able to move them around, and that
shall change the calculations. – Gnutt
1 hour ago
Please, please, don't do that. For starters, this isn't a standard method of interaction with a spreadsheet. Your end-users will likely be confused and may not even ask for clarification.
You'll want to explore the idea of data validation:
Using Data->Validation, make a drop-down menu listing all the sheets in the workbook (if the names of all the sheets are static, you can just hardcode them, otherwise, you'll need some VBA to pull them).
Then the user just picks the sheet of their choice and indirect() will automatically update everything.
Alternatively, you can also check out Tools->Scenarios. I don't know anybody who uses this feature, but you might be a good candidate for it. Basically, it lets you see the results of calculations using different datasets (i.e. "scenarios") so the user can go back and forth between them.
Using either of the 2 methods above, there's a good chance you can avoid VBA entirely, thus saving users that annoying warning message when they open your file.
=INDIRECT("Sheet"&TEXT(VALUE(MID(CELL("filename",A8),FIND("]",CELL("filename",A8))+1,256))-1,"#")&"!A1")
Caveats:
Your workbook must be saved previously
A8 may be replaced by a reference to ANY non-error cell
I know it's not seen here as good practice, but I want to do something similar. And it does replicate database functionality to an extent but I don't have the time or support to build one from scratch when there's something already half in place.
The reason I want to be able to do this is to create a summary table that links to all the worksheets in the workbook, and automatically extends if you insert a new worksheet. This is to manage a large sales / reporting spreadsheet with lots of different business units that all have the same structure (ie use the same worksheet format to report the same outcomes for different people. There is a high turnover. I want to have several summary sheets reporting different aspects of the source sheets. This is very time consuming to manage if recreating all of the tables each time.
You should be able to use the row() as an index marker to define the information that you want using something like REPLACE, OFFSET or INDEX but you can't as they only refer to 2D arrays.
Whereas Excel treats 3-D references as arrays for statistical functions it does not seem to do the same for reference functions. You might have SUM(sheetX:sheetY!A1) and be able to add a sheet in between, there is not (eg) a INDEX(sheetX:sheetY!A1,n) function. I've tried experimenting using these 2D functions as part of array formulas, and defining the 3D reference as an array or a named range... well it was worth a go :).
So I believe it's a valid action. I also believe there must be a way to do it, but for now I'm falling back on a UDF that has the risk of errors caused by calculation issues, or manipulating a Workbook_SheetChange function or similar. Or creating a single master sheet to control all the others which is populated by using a subroutine based on an array of all workbooks.
these functions work well for me. They get the worksheet index (the parent of
the range you call them with), add or subtract from that index, and then create
a range from that (relative) sheet and the address passed in.
Function relativeSheet(r As Range, iRelative As Integer)
Application.Volatile
relativeSheet = Sheets(r.Cells(1, 1).Parent.Index + iRelative).Range(r.Address)
End Function
Function prevSheet(r As Range)
prevSheet = relativeSheet(r, -1)
End Function
Function nextSheet(r As Range)
nextSheet = relativeSheet(r, 1)
End Function

Resources