How to keep named range formula from changing cell values with VBA - vba

I noticed an interesting problem while trying to debug a VBA routine that sorts the list of worksheets in a range and then redraws the border around that range.
The range containing is defined in Name Manager with the formula as shown below
=Tables!$L$2:$L$22
The problem that is occurring is that while doing the sheet name work, sometimes cells are deleted and sometimes cells are inserted. This CHANGES the cell address values in the named range. So if I deleted two cells and inserted one cell, the formula changes to
=Tables!$L$2:$L$21
And if I happen to insert into the first cell (L2), then the formula changes to
=Tables!$L$3:$L$22
I'm certain that problem can be solved using the header range name and offsetting by one, but I'm not sure how to do that in the formula for a named range as I've tried numerous ways and can't get it right. But I also need the ending range address to be static.
Any help appreciated.

One thing to try is to use:
=Indirect("Tables!$L$2:$L$22")
rather than
=Tables!$L$2:$L$22
It is an acceptable Name, but I am not sure you will get the same functionality.

Found a better way than the comment using the static header range. You simply define the whole range using the offset function from the header.
=OFFSET(AllSheetsHeader,1,0,21,1)
Where the 1,0,21,1 are 1 row offset from header, 0 column offset, 21 is row height, and 1 is just that one column

Related

Use the same formula multiple times for multiple cells containing dropdown lists

My file looks something like this
image
An IF statement would be too long considering I have 20 different long formulas.
Update: I later tried to use an IF statement but failed because it depended on substitute function to replace cell references in the equation but substitute returned a string which coudn't be used as a function by IF.
User should be able to increase available rows by simply copy and insert the previous row.
I can use index match to copy a cell's value which contain the right formula according to the dropdown list but when the user select the same item again I can't update the formula with the new values without affecting the previous cell which used the same formula.I couldn't find a way to copy the same formula several times and replace cell references in it (without human interaction like search and replace) i.e. by using Substitute function which couldn't replace cell references as it looks through the cell's value not it's formula (the cell which contain the main formula).Here is one of the 20 formulas I have
=(Tables!O167*144/(Tables!O158*Tables!O159)/4005)^2*INDEX(Tables!A159:L200;MATCH(INDEX(Tables!A159:A200;MATCH(TRUE;INDEX(Tables!A159:A200>=Tables!O158*Tables!O159/(Tables!O160*Tables!O161);0);));Tables!A159:A200;0)+MATCH(INDEX(Tables!B159:B200;MATCH(INDEX(Tables!A159:A200;MATCH(TRUE;INDEX(Tables!A159:A200>=Tables!O158*Tables!O159/(Tables!O160*Tables!O161);0);));Tables!A159:A200;0)+MATCH(INDEX(Tables!B159:B200;MATCH(TRUE;INDEX(Tables!B159:B200>=Tables!O164/(2*Tables!O158*Tables!O159/(Tables!O158+Tables!O159));0);));Tables!B159:B200;0)-1;);Tables!B159:B200;0)-1;MATCH((INDEX(Tables!C158:L158;MATCH(TRUE;INDEX(Tables!C158:L158>=Tables!O163;0);)));Tables!A158:L158;0))I tried to use FORMULATEXT to convert the formula into a string then use substitute to replace the cell references then use the depreciated Evaluation function but hit the 255 char limit. I searched a lot on google but to no avail, I don't mind a VBA code but a macro free method would be better, Thanks.
TL;DR: Is there a way to copy a formula stored in a cell and replace some of the cell references then enter it in another cell multiple times with different cell references each time ??
Try this in E2 and fill down.
=CHOOSE(MATCH(LOWER(LEFT(A2)), {"r","s","t"}, 0), B2*C2, B2^2, B2*C2/2)

VLookup table array from another sheet

Ive a sheet in which a cell value to be dynamically changed based on the cell value in the same row. So we have to pick that cell value first and then use that value to search in another sheet. Here is the code I have got. The sheet I have to search is Mapper
ActiveSheet.Range("P2").Select
formularwcount = ActiveSheet.UsedRange.Rows.Count
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-11], Mapper!C[-13]:C[2],3,FALSE)"
In this code they are using R1C1 convention which I didnt understand what they mean by putting Mapper!C[-13]:C[2] there as the table array. Im only a 10 days old developer with VBA. Anyone please tell what does that mean.
I found this information by Googling "R1C1":
Cell References
In place of a named cell, an alternative approach is to use a cell (or grid) reference. Most cell references indicate another cell in the same spreadsheet, but a cell reference can also refer to a cell in a different sheet within the same spreadsheet, or (depending on the implementation) to a cell in another spreadsheet entirely, or to a value from a remote application.
A typical cell reference in "A1" style consists of one or two case-insensitive letters to identify the column (if there are up to 256 columns: A–Z and AA–IV) followed by a row number (e.g., in the range 1–65536). Either part can be relative (it changes when the formula it is in is moved or copied), or absolute (indicated with $ in front of the part concerned of the cell reference).
The alternative "R1C1" reference style consists of the letter R, the row number, the letter C, and the column number; relative row or column numbers are indicated by enclosing the number in square brackets. Most current spreadsheets use the A1 style, some providing the R1C1 style as a compatibility option.
Source: Wikipedia: Spreadsheets

Named Cells and Formulas In Excel

How do I utilize named cell references in Excel that aren't absolute. I want to be able to take a formula and be able to drag it across excel and have one name cell reference update to a different named cell as I move across.
For example: I want to keep RevenuePerStay going across the formulas row and have excel updated the cell reference to the number of people staying. So
400 should be RevenuePerStay * Stay400
600 should be `RevenuePerStay * Stay600`
I tried using mixed cell reference and relative cell references using the dollar sign but excel will not accept this.
Assuming your stays are in row 5:
For this worksheet, click on cell C7 and go to create a new named range called Stays and for the formula write =C$5$ and exit the name manager.
Now change your formula in C7 to being RevenuePerStay*Stays and drag it across. This will get the right amount of stays you want each time.
In explicit answer to your question: no you would never get the name in the formula to change unless you put all scenarios in the formula using multiple if statements.
If I understand your question correctly, this method seems convoluted because you can use =RevenuePerStay*C5 and drag over the row, and it should give the answer you want.
If you really want to take the advantage of named range and make it change dynamically, you will need to incorporate with INDIRECT like this:
=RevenuePerStay*INDIRECT("Stay"&C5)
But this is assuming you have all the named ranges defined properly such as Stay200, Stay400, Stay600, Stay800, Stay1000 like below. Otherwise it will not work.

SUMIFS returns 0 using dynamic criteria, with criteria range and sum range on another sheet

Anyone,
I've chatted with and called excel customer service with no luck. I used the formula builder (please see attached screenshot) to make sure each element of the formula is correct and returns the value for the criteria I'm trying to reference.
Everything is accurate, but it returns a value of 0. When I do the same thing in the actual sheet the data is stored in (and click a criteria cell within the criteria range) it returns the accurate value?! I'm not sure why it won't work on the other sheet. The values I am using to select are dynamic and change with a drop down. I have another, advanced, workbook (I did not create) that does the same thing and completes an even more complicated formula, but actually works so I'm not sure why this is returning a 0 value.
Photos and code/syntax: Dynamic Selection, Example 2 of it working, Example 1 of it working, Formula Builder, CountIFs, Advanced Spreadsheet working, VLOOKUP
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,FMCOP!$C$20,'GFEBS Pull'!H:H,FMCOP!B23)
or:
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,'FMCOP'!$C$20,'GFEBS Pull'!H:H,'FMCOP'!B23)
When I type ' around FMCOP sheet name, they disappear? I've also tried to lock the columns on the 'GFEBS Pull' sheet with no luck. Cell B23 is not locked because I'm going to copy the formula down to reference other cells. Any help is appreciated!
In this screenshot you can clearly see that both FMCOP!C20 ansd FMCOP!B23 have prefacing spaces; e.g. " HHC".
Since " HHC" will never match "HHC", fix the data returned from 'the lower table in the same screenshot'.
A Text-to-Columns, Fixed Width, Finish should do this. You could adjust the original formula like,
=SUMIFS('GFEBS Pull'!Q:Q, 'GFEBS Pull'!G:G, TRIM(FMCOP!$C$20), 'GFEBS Pull'!H:H, TRIM(FMCOP!B23))
I would caution against the latter 'bandaid' fix. Fix the original data; do not apply bandaids on-the-fly.

Excel Cell reference that will go to the left

I am using an excel spreadsheet formula to add two cells together and I want to have it reference one row from left to right like it normally does, but I need it to reference the same row in another page in reverse, and then be able to drag the formula through the entire worksheet. I cannot seem to find anything like this when I look online. TLDR I need to know how to tell excel reference the cell to the left if I drag it to the right.
so I need to add A0 in page one and A9 in page two then when I copy the formula to the left I need it to grab A1 and A8, is there a way to get this?
Your question is not very clear; for example, you talk about a formula but have an Excel-VBA tag. However, I think I understand the effect you seek.
I have filled A1:I1 of the current sheet and A1:I1 of worksheet “Sheet3” with numbers.
I have typed the following formula into another cell within the current worksheet:
=A1+INDEX(Sheet3!$A$1:$I$1,,10-COLUMN(A1))
The target area for the INDEX function is Sheet3!$A$1:$I$1. This is a one row range so I do not need a row number. The column number is 10-COLUMN(A1) which is 9 so this formula is equivalent to:
=A1+Sheet3!I9
If I copy the formula one cell to the right I get:
=B1+INDEX(Sheet3!$A$1:$I$1,,10-COLUMN(B1))
This is the equivalent of:
=B1+Sheet3!H9
You can continue copying the formula to the right until you get:
=I1+INDEX(Sheet3!$A$1:$I$1,,10-COLUMN(I1))
Attempting to copy further gives an error because 10-COLUMN(J1) is zero and there is no column 0.
There are other methods of achieving this effect. The key feature of any such method is the expression N-X where N is a constant and X gets bigger as the formula is copied further to the right.