Excel - get range of data between two rows - excel

I have data in following pattern:
I want a formula to get the data between Content Start and Content End. The amount of content between the start and end is not just limited to 4 and can change.

You don't say what you want to do with this result.
I am assuming you can code the column in which your data is located.
The formula below will return those values as an array. e.g. with your data, the formula will return {1;2;3;4}
The values could be numeric or text.
How you want to handle those values is up to you.
=INDEX($A:$A,N(IF(1,ROW(INDEX($A:$A,MATCH("Content Start",$A:$A,0)+1,1):INDEX($A:$A,MATCH("Content End",$A:$A,0)-1,1)))))
EDIT: Here is an example of using that information to create a sparkline. In this example, the formula above has been wrapped in TRANSPOSE and entered as an array formula in C1:I1. The Sparkline has been entered in B1. As with most charts, Excel is ignoring the #N/A errors.
And here is an example where the sparkline is in A1 and the transposed array is somewhere else, not in view, on the worksheet:

With Worksheets("Your Sheet").Range("A:A")
Set c = .Find("Content Start", LookIn:=xlValues)
Set d = .Find("Content End", LookIn:=xlValues)
ActiveSheet.Range(Cells(c.Row + 1, c.Column), Cells(d.Row - 1, c.Column)).Select
End With

Probably multiple ways doing this, but if you prefer to do it without VBA you could use:
Formula used in B1 (allthough a bit lengthy):
{=INDEX($A$1:$A$10,SMALL((ROW($A$1:$A$10)>MATCH("Content Start",$A$1:$A$10,0))*(ROW($A$1:$A$10)<MATCH("Content End",$A$1:$A$10,0))*ROW($A$1:$A$10),SUM((ROW($A$1:$A$10)>MATCH("Content Start",$A$1:$A$10,0))*(ROW($A$1:$A$10)<MATCH("Content End",$A$1:$A$10,0)))+ROW(A3)))}
Notice it's an array formula entered through CtrlShiftEnter
Drag down....

Related

Auto fill specific cell range with formulas

I have this formula:
IF(ROWS($Q$27:Q27)<=$P$25,INDEX(DataTable[[#All],[Time]],$P27),"")
and if I drag it to the right, it should automatically read each column respectively; example:
=IF(ROWS($Q$27:R27)<=$P$25,INDEX(DataTable[[#All],[Name]],$P27),"")
^Notice that the first Q27 is fixed, the second Q27 is variable.
I drag this formula to the right by 15 columns, and down to 50 rows. that's 750 formulas in total.
I want to do this in vba, but if I did this, it will be 750 lines of code for each cell representing each row/column.
example: .Range("G17").Formula=IF(ROWS($Q$27:R27)<=$P$25,INDEX(DataTable[[#All],[Name]],$P27),"""")
and if I drag it down, it will automatically pick up what I exactly want, example:
=IF(ROWS($Q$27:Q28)<=$P$25,INDEX(DataTable[[#All],[Time]],$P28),"")
so this formula should be written 750 times in total for the cell range [ A27:N76 ]
Any faster / more dynamic approach? and if possible, can I make it depend on more than 50 lines based on a cell value inside the sheet?
Example:
This should do it all in one line:
Range("A27:N76").FormulaR1C1 = "=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX((DataTable[[#All],[Name]],RC16),"""")"
EDIT: Seems a more that one line of code required after all 😊
The code below will do what you want (this time fully tested)
Sub FillFormulas()
Dim inC%, rgHead As Range
''' Assumes the target sheet is Active.
''' o If that's not the case, change this With statement to reference the target sheet
With ActiveSheet
''' Set rgHead to the Table's header row
Set rgHead = .ListObjects("DataTable").Range.Rows(1)
''' Add the formulas to the target range, column by column updating the table header on the fly
With .Range("A27:N76")
For inC = 1 To .Columns.Count
.Columns(inC).FormulaR1C1 = _
"=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX(DataTable[[#All],[" & rgHead.Cells(inC) & "]],RC16),"""")"
Next inC
End With
End With
End Sub
so this formula should be written 750 times in total for the cell range [A27:N76]
You don't need to do that. If you specify range.Formula, it will fill the proper formulas all the way across and down. Just give it the formula of the top/left most cell.
So, in your case
Range("A27:N76").Formula = "=IF(ROWS($Q$27:R27)<=$P$25 ... "
EDIT: This response had some obvious errors
This has an obvious error (as tested part and then merged to the full thing).
Range(A27:N76).FormulaR1C1 = "=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX((DataTable[[#All],[Name]],$P27),"""")"

How to only run a formula if the cell is blank

I am writing a formula to run a matching analysis on some elements of a spreadsheet. The column containing the formula already has values filled in, but I need to run the formula on the blank cells. The formula is working but overwriting the current info I have within the filled cells.
I have tried a VBA code which got too complicated and did not work. With VBA I tried inserting the formula into only the blank spaces but was having some issues. I have also tried a basic excel formula.
=IF(COUNTIFS(A:A,A2,BU:BU,"CLOUD")=0,"Not Cloud",IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud","Hybrid"))
VBA Code I tried:
If (IsEmpty(Bucket) Or Bucket = "" Or Bucket = vbNullString) And _
(GetCellValue(.Cells(i, "BU")) = "CLOUD") Then
Range("CC:CC").Formula = "=IF(COUNTIFS(A:A,A2,BU:BU,""CLOUD"")=0,"Not Cloud", _
IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud"","Hybrid"))"
A reminder this is a high-level version
I want that formula to only run in the cells that do not have a value already in place. It should not overwrite the data from the VBA script that I previously ran before doing the formula. The disposition column should be completely full of values. The formula needs to run in only the blank cells.
You could achieve this with an additional column.
In the column to the right of Disposition (insert one if necessary) write the following formula (example for row 2, assuming Disposition is Column C):
=IF(C2=""; [Your formula here]; C2)
This will provide you with a column full of values, anytime you put a value into column C it will override the formula.
Edit: You can then copy this formula down the entire column to achieve the desired effect.

Using IF Function Range in Excel

I have some problem in using If Excel function. I want to use range in excel. So when my data is in range, it will show something. I use this formula but it doesn't work "
=IF(E11=Rekap.C8:C21, VLOOKUP(Rekap.C8:C21, Master.A2:C148, 3), "")
Form that formula, if data in E11 is same like data in range C8 - C21 in Rekap sheet, it will show another data in Master sheet that in range A2-C148 column 3. How can I use range in if formula ?
A single cell like E11 cannot be compared to a range like C8:C21.
I assume that you really want to check if the value in E11 appears anywhere in the range C8:C21 and if so, perform the lookup. That can be done in several ways
=if(isnumber(match(e11,Rekap!$C$8:$C$21,0)),vlookup(E11,Master!$A$2:$C$148,false),"")
Note that I added the fourth parameter to the Vlookup. It will default to TRUE if omitted, which may return wrong results if the lookup table is not sorted.

Excel: Adding a row disrupts formula

In a spreadsheet I use for cash management tracking, I have the following formula:
=IF(D176="Cash", F175+C176, IF(D176="Transfer", F175+C176, F175))
When I add a row, I use control+D to fill in the formula from the cell above (I'm using Excel for Mac 2011). This results in the correct formula as follows:
=IF(D177="Cash", F176+C177, IF(D177="Transfer", F176+C177, F176))
However, this has the effect of changing the formula in the cell in the row below:
=IF(D178="Cash", F176+C178, IF(D178="Transfer", F176+C178, F176))
Here you can see the rows for column F are not correct: F176 should be F177.
Can anyone offer any advice to ensure that when I insert a row the formula remains intact?
Thanks.
Replace all of the references to F175 in the original formula (the one if row 176) with INDEX(F:F, ROW()-1).
=IF(D176="Cash", INDEX(F:F, ROW()-1)+C176, IF(D176="Transfer", INDEX(F:F, ROW()-1)+C176, INDEX(F:F, ROW()-1)))
'or better as
=INDEX(F:F, ROW()-1)+(OR(D176={"Cash", "Transfer"}*C176)

Array Formula To Return List Based on Multiple Conditions

I have a spreadsheet that looks like the picture below. I have some formulas that perform counts on this sheet and compare this data to another data source. An example of one of these would be:
=Countifs(A2:A10676,"0",C2:C10676,"OPEN",D2:D10676,"Current")
How would I return the list of loan numbers associated with the count? For instance, the count above returns 3038. I tried the below formula. I selected C2:C3039, typed the formula and hit Shift + Ctrl + Enter:
=If(And(A2:A10676="0",C2:C10676="OPEN",D2:D10676="Current"),B2:B10676,"")
My thought was that this evaluates the logical for each line and builds an array of the values in column B. When I enter this, each cell in the array is blank.
Can someone please explain how to return an array with values based on multiple criteria?
I see two problems with your formula
=If(And(A2:A10676="0",C2:C10676="OPEN",D2:D10676="Current"),B2:B10676,"")
Firstly you can't use AND to return an array - AND (like OR) returns a single result (TRUE or FALSE), so you need either nested IFs or to use * to simulate AND, i.e. either
=IF(A2:A10676=0,IF(C2:C10676="OPEN",IF(D2:D10676="Current",B2:B10676,"")))
....or.....
=IF((A2:A10676=0)*(C2:C10676="OPEN")*(D2:D10676="Current"),B2:B10676,"")
Note: I used 0 without quotes for the first criteria - for COUNTIFS you can use "0" or 0 but here it needs to match the data type - only use quotes if the data is text formatted - I'm assuming that isn't the case
The second problem is that for both of those the resulting array still has 10675 values because it still includes all the blanks for rows when the criteria aren't met.
To get an array of just 3038 values you can use this formula array entered into the correct sized range:
=INDEX(B2:B10676,SMALL(IF(A2:A10676=0,IF(C2:C10676="OPEN",IF(D2:D10676="Current",ROW(B2:B10676)-ROW(B2)+1)),ROW(INDIRECT("1:"&E2))))
Where E2 contains your COUNTIFS formula
Note that this only works to return an array in worksheet range - it won't work to return an array to be used in another function
Here is an approach that uses a "helper column" rather than an array formula:
In E2 enter the formula:
=IF(AND(A2=0,C2="OPEN",D2="Current"),1+MAX($E$1:E1),"")
and copy down (this marks the multiple rows meeting the criteria)
In F2 enter:
=IFERROR(INDEX(B$2:B$24,MATCH(ROWS($1:1),$E$2:$E$24,0)),"")
and copy down.
You can set a data filter:
Dim wrk As Worksheet
Set wrk = ActiveSheet
If Not wrk.AutoFilterMode Then wrk.range("a1").AutoFilter
wrk.range("a1").AutoFilter field:=1, Criteria1:="0"
wrk.range("a1").AutoFilter field:=3, Criteria1:="OPEN"
wrk.range("a1").AutoFilter field:=4, Criteria1:="Current"
Set wrk = Nothing
Of course, you could manually turn on the filters to how you like, too.
Insert four blank rows at the top, copy A5:D6 into A1 and delete B2. DATA > Sort & Filter, Advanced, Copy to another location, List range: A5:D10680, Criteria range: A1:D2, Copy to: F1:I1, check Unique records only, OK.

Resources