Excel/FORMULAS

Advertisement


Question
Column  A   B   C   D   E          
Row 2   X   
Row 3      X
This is how the spreadsheet looks with the lines.  Assume that Column (5) has the below "x" inside. I would like to create a formula that will show column A2 thru H2, if I inpute an "x" inside either column cell it will return with a specific number e.g.  A2=5, B2=4, C2=3, D2=2 and likeso.  Thanks for your help?

Answer
Hi Tanya,

If I understand your question correctly, there is no simple combination of existing worksheet functions that will do this.  For this reason I wrote a simple user-defined function (UDF) to do it:

Function FindX(R As Range) As Integer
  'returns the inverse index of the first "X" found in the range R
  Dim i    As Integer
  For i = 1 To R.Cells.Count
     If Ucase(R.Cells(i)) = "X" Then GoTo FoundX
  Next i
  FindX = 0   'X not found
  Exit Function
FoundX:
  FindX = R.Cells.Count - i + 1
End Function

Simply place this code in a standard macro module in your workbook*.

Then you can use the function like this:

=FindX(A2:H2)       (this should yield a value of 5 if the X is in cell A2, 4 if in B2, etc.)

Note that if there is no X in the specified range the function will yield 0.  If there are multiple Xs, FindX will yield the number for the first (leftmost) X found.  Note also that you can use either upper or lower case X, but FindX will look for no other character.

I hope you find this helpful.

Damon


* to place the code in a macro module, go to the Visual Basic Editor (keyboard Alt-TMV), then insert a new macro module (Alt-IM), then paste the code into the Code pane.

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Damon Ostrander

Expertise

I have extensive experience with VBA programming in Excel 5 through Excel 2013. As a former aerospace engineer with a large aerospace corporation and consultant in a small defense technology services company, I have developed a wide range of applications in VBA, including simulations involving mixed-language programming, satellite orbit mechanics, graphics and animation, and real-time applications. I am interested in moderate to hard VBA-related questions only.

Experience

I have developed and taught several courses in Excel VBA programming and also VBA programming in Office 97, 2000, and 2007. I have developed a number of large technical applications in Excel VBA for use within the aerospace industry.

Education/Credentials
B.S. in Electrical Engineering and Computer Science, University of California, Berkeley.

©2016 About.com. All rights reserved.