Naming a dynamic cell range based on another worksheet. - dynamic

I want to set up a dynamic cell range, which varies length and start point of the range based on user inputs.
I've named a range using the following formula:
=OFFSET(INDIRECT("P"&'FTB data (ML2)'!$N$420),0,0,'FTB data (ML2)'!$N$422,1)
Where:
FTB data (ML2) is the worksheet with the source of data
P is the start column of the data
N420 contains an input for the start row of the range
N422 contains the input for the length of the range
Now this all seems to more or less work, the length varies according to the value in N422 and the start varies based on N420.
The problem is that it's not fixed to the FTB data (ML2) worksheet. I.e when I change tabs to my Summary Sheet, it draws from the range in the summary sheet, no tthe FTB data sheet.
Does anybody have any idea how to make sure it only draws from FTB data, and not the currently active sheet?
Thanks!

I managed to get it working with the following formula:
=OFFSET(INDIRECT("'" & "FTB Data (ML2)" & "'!" & "P"&Summary!$C$1),0,0,'FTB data (ML2)'!$N$422,1)
Turns out you just need a few more ' and " floating about to get an INDIRECT to reference another worksheet correctly.

Related

Blank cells in matrix take on average of surrounding cells in Excel

I have a matrix of numbers in excel that contains several blank cells. The numbers correspond to the clay level in the ground, as indicated by a borehole in that position. I am using the cells as a grid to represent a map. I want to replace the blank cells with an average of all of the cells which surround it. How do I do this? I keep getting circular reference errors when I try to do it.
1) Use a proxy range: Create a duplicate range and have that refer to the original. The named range here is F5:H6
For above the range on the left (F5:H6) is called namedRange and the blank cell in the duplicate range on the right uses Average(namedRange) to populate the value.
To add a named range , select your range and then add the name via the name manager .
OR
2) Use VBA
With VBA using the same named range (code in a standard module - replace activesheet with your sheetname) :
Option Explicit
Sub test()
With ActiveSheet.Range("namedRange")
.SpecialCells(xlCellTypeBlanks) = Application.WorksheetFunction.Average(.Value)
End With
End Sub

Data validation excel offset of current cell for two columns SNR and REG

I am trying to with data validation in excel to get two columns in one column so to speak. In the column I have circled blue I want to have the user pick Reg or SNR (circled red) and it will get the offset of the current cell. I have tried [![=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,5)][1]][1] which works as the cell is always 5 to right. However it does not get the values under that but only that one cell. It also of course excludes Reg values.
Is what I am after possible in Excel or do I have to do up a new table to incorporate SNR?
create a named range for SNR and REG and use the below as the formula in the data validation rule:
=INDIRECT($O$5)
You can make the named range for REG dynamic like using the below:
=OFFSET($U$6,0,0,COUNTIF($U$6:$U$6000,">"&0),1)
And simply change the range and do the same for SNR
If I understand you correctly, this is what you are trying to do but let me know if this is not what you intended to do.
First, under cell O5, I set up a Data Validation like the pic above so the end user can pick either SNR or Reg.
Second, I used this OFFSET formula inside Data Validation to display the possible values under either column T or U based on the selection on cell O5:
=IF(O5="SNR",OFFSET($T$6,0,0,COUNTIF($T$6:$T$6000,">"&0),1),OFFSET($U$6,0,0,COUNTIF($U$6:$U$6000,">"&0),1))

Set Value of Cell, relative to active cell, within single column named range

I have written the following code as part of a larger sub-routine to set the value of a cell, relative to the active cell, when a particular selection has been made within the active cell.
ActiveCell.Offset(0, 5).Value = "CLV 7"
While this works, I may have need in the future to add columns into my worksheet and this presents a problem, due to the change of location of the cell that requires its value to be set, and by association the need to rewrite the code each time a new column is added.
In considering this variable, I researched and as a result, defined a range name for each column that requires values to be set within it. I thought that I would then be able to determine the variable & relocatable intersect point between the active row and the named range column and define it as the cell that requires the value to be set.
After this I researched ways to define this variable intersection and attempted to set the following alternate code:
ActiveCell.Offset(0, 0).Range("BusinessStudies").Value = CLV 7
in the hope that it would do the trick, but unfortunately it does not. I have looked at other posts and cannot see how to adjust it with any success as I can't see any similar requests.
try the Intersect() function in VBA
Debug.Print Intersect(Rows(ActiveCell.Row), Range("MyRange")).Value
Edit: apply to your situation, assuming that you want the string "CLV 7" to go into the cell:
Intersect(Rows(ActiveCell.Row), Range("BusinessStudies")).Value = "CLV 7"

get multiple column names (header) in table associated with particular value in to a cell

i need to get multiple column names (header) in table associated with particular value in to a cell
as i explained, i need to get the heading names corresponding to value "n" to column E.
i used the formula
=INDEX((A$1:D$1),MATCH("n",A2:D2,0))
here. but it only give one column name.
i am open to vba scripts also. but i think it doesn't need vba. just improve the the above formula, may be. i tried and failed. any help. thank you guys
if you are really "open" to vba, I'll use one simple UDF like:
Function HeatherNames(rg As Range, rf As String) As String
For Each cell In rg
If cell = rf Then HeatherNames = HeatherNames & Cells(1, cell.Column).Value & "-"
Next cell
HeatherNames = Left(HeatherNames, Len(HeatherNames) - 1)
End Function
you can use it in the column E `=HeatherNames(A2:D2;"n") now you can select the arg.1 (range) and type (or referring to another cell) the arg.2
Assuming you have Excel 2010 or later, in E2:
=IF(COLUMNS($A:A)>COUNTIF($A2:$D2,"n"),"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
Copy to the right and down as required.
It would actually be slightly more efficient (and certainly if your dataset in reality is quite large) to have the initial IF clause held within its own cell, such that it is calculated for each row only once, rather than for each instance of the formula within that row. So a better set-up would be, in E2:
=COUNTIF($A2:$D2,"n")
copied down. Then, in F2:
=IF(COLUMNS($A:A)>$E2,"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
copied to the right and down again.
Regards

Keeping column formulas in a table header

Is it possible to have the formulas that I need applied on columns be saved or applied to a column header or some kind of metadata so that as and when I add new rows to my Excel table the Formulas get applied to the columns?
Scenarion:
I am creating a template Table, which will have no rows at first.
On a separate sheet (or same sheet for that matter) once the user selects the number of rows to be generated in the table, I dynamically add rows to the table using VBA.
The idea is I may not have any rows in the table at beginning OR user may have deleted rows manually.
When I programmatically add new rows, I want the Formulas applied on the cells as well. Most of the formulas I am using are either of the three types:
Structured table reference, Excel functions like SUM, AVERAGE etc and custom function names.
Updated:
Here is what I have tried:
1> tried applying the formula to the header itself.
Result: The header it self changes with #REF! error. I think the behavior is correct. So it's a no-go option.
2> Tried creating one row and apply the formula to the row. That works, but the problem is, I do not want a dummy row to begin with.
3> Using VBA code to add row to the table using
ActiveWorkbook.Worksheets("Sheet3").ListObjects("Table2").ListRows.Add AlwaysInsert:=True
inside a for loop.
The new rows retain the visual style sheets, but does not seem to retain the formulas. Just blank cells.
Could the fomrmulas be in header cell commnets?
And then with VBA add the formula for the current row:
Sub test()
Dim headerCells As Range
Set headerCells = Range("B2:E2")
OnNewRow 3, headerCells
End Sub
Sub OnNewRow(newRow As Integer, headerCells As Range)
Dim headerCell As Range, targetCell As Range, formulaFromComment As String
For Each headerCell In headerCells
formulaFromComment = GetFormulaFromComment(headerCell)
If (formulaFromComment = "") Then _
GoTo NextHeaderCell
Set targetCell = Intersect(headerCells.Worksheet.Rows(newRow), _
headerCell.EntireColumn)
AddFormula newRow, targetCell, formulaFromComment
NextHeaderCell:
Next
End Sub
Sub AddFormula( _
newRow As Integer, _
targetCell As Range, _
formula As String)
formula = Replace(formula, "{ROW}", newRow)
targetCell.formula = formula
End Sub
Function GetFormulaFromComment(headerCells As Range) As String
' TODO
GetFormulaFromComment = "=SUM($C${ROW}:$E${ROW})"
End Function
Just use tables.
If you highlight cells and choose Insert Table from the ribbon, it doesn't just give you formatting and filters. It also, if you build them the right way, stores column formulas once per column instead of once per cell. Also, the formulas are more readable!
For formulas, you can't use cell addresses if you want it to be a single column formula unless they are absolute. (E.g. $A$1, not A1.) Instead, you use [ColumnTitle] for the entire column (where "ColumnTitle" is the actual title of that column) and [#ColumnTitle] for the column value in the same row. So if "Cost" was the title of column B, "RunningTotal" was the title of column C and your formula for C6 was therefore =B6+C5, you'd instead use a formula of =[#Cost]+OFFSET([#RunningTotal],-1,0)] which is longer but much easier to read/maintain/debug, and if you change a column title then the formulas change too! No VBA required. Given this, plus being able change columns for the entire columns at once, plus being able to refer to other columns in other tables without worrying about cell addresses (e.g. MAX(Table1[Cost])), plus being able to style the tables so easily, plus the integration with Power-Query, and VBA support. (See learn.microsoft.com.) Whether VBA or otherwise, add a row to your table and the columns with a single column formulas will automatically carry over into the new row.
Not sure about Table templates or VBA but perhaps there is another option by using =ARRAYFORMULA()
For example, say you had a header row and 3 columns and wanted your last column to be the product of the first two. In cell C2 you could enter the following:
=ARRAYFORMULA(A2:A*B2:B)
This has three benefits:
Skips the first row completely
Effectively applies the formula to every row which is useful if you later decide to insert a row (your question)
Only one location to modify the formula for every single row
Although, it may not be immediately obvious where how/where the cells are being calculated. (hint: ctrl+~ may help)