1

Looking for help

 A       B       C       D       E        F
1               Food    Petrol   Rent   Wages
2       **Food** 12
3       **Petrol**        35
4       **Rent**                  120
5       **Wages**                       300

I have a formula that copies values into cells C2:F5 -onwards but I'm having difficulty coming up with a formula that copies the column header "Food/Petrol/Rent" into the B column if there is a value in the appropriate cell in the column

So if C2 has a value then B2=C1 If F5 has a value then B5= "Wages"

currently I have 38 Columns and nearly 1000 Rows, it could get complicated to do it all manualy

Thanks in advance

4
  • Each row of values (C2:F5) should only have 1 cell with a value in it. nothing should be 'Food' AND 'Petrol' Mar 4, 2018 at 5:32
  • Will the cell value always be a number? Can there be a row with no value?
    – fixer1234
    Mar 4, 2018 at 6:59
  • the value will always be a number yes, every row should have exactly one value, Mar 4, 2018 at 7:16
  • Thanks for your help, I fear I gave you incomplete information for my problem, I have reworded the problem here superuser.com/questions/1300454/… With more details, Thanks. Mar 5, 2018 at 1:38

1 Answer 1

1

There are a number of ways to do this. You describe having 38 columns, so some form of lookup is the simplest approach. You can do this with INDEX and MATCH:

enter image description here

The formula in B2:

=INDEX($C$1:$F$1,MATCH(0,ISBLANK(C2:F2),0))

Notice the braces around the formula in the image. This is an array formula, so you need to enter it using Ctrl + Shift + Enter instead of just Enter. And of course, you need to adjust the ranges to match your data.

How it works:

MATCH looks for the first nonblank entry, in which case ISBLANK will return False, which OpenOffice displays as 0 (True would display as 1). MATCH returns the column position within the range where the match was found. The 0 parameter at the end looks for an exact match.

INDEX returns the value from the first row in the corresponding column position found by MATCH.

You can copy and paste the formula down column B once it's entered.

UPDATE:

You've indicated in comments that your actual spreadsheet doesn't have empty cells where there are no numbers. Rather, they contain formulas that display a blank cell. In this case, ISBLANK won't recognize it as blank. We can handle that by just testing for a number value and looking for the first True condition. So the B2 formula would be:

=INDEX($C$1:$F$1,MATCH(1,ISNUMBER(C2:F2),0))

Again, this is an array formula, so you need to enter it using Ctrl + Shift + Enter instead of just Enter. I just verified that this formula works in this example when the "empty" cells aren't actually blank.

3
  • Thanks for your help, but I fear I wasn't complete enough with the information I gave you, Mar 5, 2018 at 0:46
  • Did this answer work for the example in this question, and then you had a problem translating it to your actual worksheet? To diagnose the problem, first ensure you have properly implemented the simplified example. That will show whether the problem is in implementing the formula or there is hidden complexity in your worksheet that the formula doesn't handle.
    – fixer1234
    Mar 5, 2018 at 2:24
  • I recreated the simple spreadsheet and the formula works, Many thanks, however the cells in the search range have a formula in them, so ISBLANK is giving false positives, Mar 5, 2018 at 5:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .