Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Excel formula

Posted on 2011-02-19
Medium Priority
297 Views
Hello Experts,

I am new to excel and need to create a formula, but not sure how to do it. I need to do this: if data in column A starts with the letter 'A' and is 9 characters or less, display cell data. If not, then blank.

Thanks.

0
Question by:erp1022
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 24

Expert Comment

ID: 34933355
Put this in B1 (or wherever) and copy down:

=IF(AND(LEFT(A1,1)="A",LEN(A1)<10),A1,"")
0

Author Comment

ID: 34933503
Sorry, now that I see your formula I see that there's an issue here that I didn't actually think would be an issue. I thought the response would be to use something similar to char_index in SQL. So here's the deal: the values in Column A are indented. Sometimes it's 30 spaces, sometimes 40, sometimes 25. It's random, this is the way it comes out of the database depending on the type of data it is. Is there a way I can throw a trim function into your formula so that it will work?

Thanks.
0

LVL 14

Expert Comment

ID: 34933545
=IF (AND(LEFT(TRIM(A1,1))="A",LEN(TRIM(A1))<10),A1,"")
0

LVL 14

Accepted Solution

svgmuc earned 900 total points
ID: 34933549
Sorry, shifted parenthesis:

=IF (AND(LEFT(TRIM(A1),1)="A",LEN(TRIM(A1))<10),A1,"")
0

LVL 93

Assisted Solution

Patrick Matthews earned 100 total points
ID: 34933803
You could certainly use TRIM, but that brings its own issues: TRIM will remove any trailing spaces, as well as reduce internal spaces of 2+ spaces down to a single space.  That may muck up your rules.

If that will be a problem, you might want to consider Regular Expressions.  For example, you could add this UDF to your VBA project:

``````Function RegExpFindSubmatch(LookIn As String, PatternStr As String, Optional MatchPos, _
Optional SubmatchPos, Optional MatchCase As Boolean = True, _
Optional MultiLine As Boolean = False)

' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
' as long as you properly credit and attribute authorship and the URL of where you
' found the code

' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

' This function relies on the VBScript version of Regular Expressions, and thus some of
' the functionality available in Perl and/or .Net may not be available.  The full extent
' of what functionality will be available on any given computer is based on which version
' of the VBScript runtime is installed on that computer

' This function uses Regular Expressions to parse a string (LookIn), and return "submatches"
' from the various matches to a pattern (PatternStr).  In RegExp, submatches within a pattern
' are defined by grouping portions of the pattern within parentheses.

' Use MatchPos to indicate which match you want:
' MatchPos omitted               : function returns results for all matches
' MatchPos = 1                   : the first match
' MatchPos = 2                   : the second match
' MatchPos = <positive integer>  : the Nth match
' MatchPos = 0                   : the last match
' MatchPos = -1                  : the last match
' MatchPos = -2                  : the 2nd to last match
' MatchPos = <negative integer>  : the Nth to last match

' Use SubmatchPos to indicate which match you want:
' SubmatchPos omitted               : function returns results for all submatches
' SubmatchPos = 1                   : the first submatch
' SubmatchPos = 2                   : the second submatch
' SubmatchPos = <positive integer>  : the Nth submatch
' SubmatchPos = 0                   : the last submatch
' SubmatchPos = -1                  : the last submatch
' SubmatchPos = -2                  : the 2nd to last submatch
' SubmatchPos = <negative integer>  : the Nth to last submatch

' The return type for this function depends on whether your choice for MatchPos is looking for
' a single value or for potentially many.  All arrays returned by this function are zero-based.
' When the function returns a 2-D array, the first dimension is for the matches and the second
' dimension is for the submatches
' MatchPos omitted, SubmatchPos omitted: 2-D array of submatches for each match.  First dimension
'                                        based on number of matches (0 to N-1), second dimension
'                                        based on number of submatches (0 to N-1)
' MatchPos omitted, SubmatchPos used   : 2-D array (0 to N-1, 0 to 0) of the specified submatch
'                                        from each match
' MatchPos used, SubmatchPos omitted   : 2-D array (0 to 0, 0 to N-1) of the submatches from the
'                                        specified match
' MatchPos used, SubmatchPos used      : String with specified submatch from specified match

' For any submatch that is not found, the function treats the result as a zero-length string

' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).

' If you use this function in Excel, you can use range references for any of the arguments.
' If you use this in Excel and return the full array, make sure to set up the formula as an
' array formula.  If you need the array formula to go down a column, use TRANSPOSE()

' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
' where a large number of calls to this function are made, making RegX a static variable that
' preserves its state in between calls significantly improves performance

Static RegX As Object
Dim TheMatches As Object
Dim Mat As Object
Dim Answer() As String
Dim Counter As Long
Dim SubCounter As Long

' Evaluate MatchPos.  If it is there, it must be numeric and converted to Long

If Not IsMissing(MatchPos) Then
If Not IsNumeric(MatchPos) Then
RegExpFindSubmatch = ""
Exit Function
Else
MatchPos = CLng(MatchPos)
End If
End If

' Evaluate SubmatchPos.  If it is there, it must be numeric and converted to Long

If Not IsMissing(SubmatchPos) Then
If Not IsNumeric(SubmatchPos) Then
RegExpFindSubmatch = ""
Exit Function
Else
SubmatchPos = CLng(SubmatchPos)
End If
End If

' Create instance of RegExp object

If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = PatternStr
.Global = True
.IgnoreCase = Not MatchCase
.MultiLine = MultiLine
End With

' Test to see if there are any matches

If RegX.Test(LookIn) Then

' Run RegExp to get the matches, which are returned as a zero-based collection

Set TheMatches = RegX.Execute(LookIn)

' If MatchPos is missing, user either wants array of all the submatches for each match, or an
' array of all the specified submatches for each match.  Build it and assign it as the
' function's return value

If IsMissing(MatchPos) Then

' Return value is a 2-D array of all the submatches for each match

If IsMissing(SubmatchPos) Then
For Counter = 0 To TheMatches.Count - 1
Set Mat = TheMatches(Counter)

' To determine how many submatches there are we need to first evaluate a match.  That
' is why we redim the array inside the for/next loop

If Counter = 0 Then
ReDim Answer(0 To TheMatches.Count - 1, 0 To Mat.Submatches.Count - 1) As String
End If

' Loop through the submatches and populate the array.  If the Nth submatch is not
' found, RegExp returns a zero-length string

For SubCounter = 0 To UBound(Answer, 2)
Answer(Counter, SubCounter) = Mat.Submatches(SubCounter)
Next
Next

' Return value is a 2-D array of the specified submatch for each match.

Else
For Counter = 0 To TheMatches.Count - 1
Set Mat = TheMatches(Counter)

' To determine how many submatches there are we need to first evaluate a match.  That
' is why we redim the array inside the for/next loop.  If SubmatchPos = 0, then we want
' the last submatch.  In that case reset SubmatchPos so it equals the submatch count.
' Negative number indicates Nth to last; convert that to applicable "positive" position

If Counter = 0 Then
ReDim Answer(0 To TheMatches.Count - 1, 0 To 0) As String
Select Case SubmatchPos
Case Is > 0: 'no adjustment needed
Case 0, -1: SubmatchPos = Mat.Submatches.Count
Case Is < -Mat.Submatches.Count: SubmatchPos = -SubmatchPos
Case Else: SubmatchPos = Mat.Submatches.Count + SubmatchPos + 1
End Select
End If

' Populate array with the submatch value.  If the submatch value is not found, or if
' SubmatchPos > the count of submatches, populate with a zero-length string

If SubmatchPos <= Mat.Submatches.Count Then
Answer(Counter, 0) = Mat.Submatches(SubmatchPos - 1)
Else
Answer(Counter, 0) = ""
End If
Next
End If

' User wanted the info associated with the Nth match (or last match, if MatchPos = 0)

Else

' If MatchPos = 0 then make MatchPos equal the match count.  If negative (indicates Nth
' to last), convert to equivalent position.

Select Case MatchPos
Case Is > 0: 'no adjustment needed
Case 0, -1: MatchPos = TheMatches.Count
Case Is < -TheMatches.Count: MatchPos = -MatchPos
Case Else: MatchPos = TheMatches.Count + MatchPos + 1
End Select

' As long as MatchPos does not exceed the match count, process the Nth match.  If the
' match count is exceeded, return a zero-length string

If MatchPos <= TheMatches.Count Then
Set Mat = TheMatches(MatchPos - 1)

' User wants a 2-D array of all submatches for the specified match; populate array.  If
' a particular submatch is not found, RegExp treats it as a zero-length string

If IsMissing(SubmatchPos) Then
ReDim Answer(0 To 0, 0 To Mat.Submatches.Count - 1)
For SubCounter = 0 To UBound(Answer, 2)
Answer(0, SubCounter) = Mat.Submatches(SubCounter)
Next

' User wants a single value

Else

' If SubmatchPos = 0 then make it equal count of submatches.  If negative, this
' indicates Nth to last; convert to equivalent positive position

Select Case SubmatchPos
Case Is > 0: 'no adjustment needed
Case 0, -1: SubmatchPos = Mat.Submatches.Count
Case Is < -Mat.Submatches.Count: SubmatchPos = -SubmatchPos
Case Else: SubmatchPos = Mat.Submatches.Count + SubmatchPos + 1
End Select

' If SubmatchPos <= count of submatches, then get that submatch for the specified
' match.  If the submatch value is not found, or if SubmathPos exceeds count of
' submatches, return a zero-length string.  In testing, it appeared necessary to
' use CStr to coerce the return to be a zero-length string instead of zero

If SubmatchPos <= Mat.Submatches.Count Then
RegExpFindSubmatch = CStr(Mat.Submatches(SubmatchPos - 1))
Else
RegExpFindSubmatch = ""
End If
End If
Else
RegExpFindSubmatch = ""
End If
End If

' If there are no matches, return empty string

Else
RegExpFindSubmatch = ""
End If

Cleanup:
' Release object variables
Set Mat = Nothing
Set TheMatches = Nothing

End Function
``````

It comes from my article http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

Use it in a formula like this:

=RegExpFindSubmatch(A2,"(^ *)(A.{0,8}\$)",1,2,FALSE)

That will return the value if the conditions are met, and an empty string if not.  The pattern, translated:

First, match the beginning of input and zero or more spaces.  (This is the first submatch)

Next, match the letter A followed by zero to eight characters, and then the end of input.  (This is the second submatch)

The formula returns the 2nd submatch.  The FALSE indicates that the pattern matching is NOT case sensitive.  Either omit that or make it TRUE to make it case sensitive.
0

Author Closing Comment

ID: 34937196
Hi, I want to thank both of you experts for your assitance. I assigned the points the way I did because svgmuc answered completely first and in the clearest and easiest manner for me. MatthewsPatrick, while your solution looks like it would work, it is way over my head. As I said in my initial request, I am new to Excel and your solution I would not even know how to begin to implement. But thank you for offering assistance.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Suggested Courses
Course of the Month6 days, 1 hour left to enroll

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.