Wonderwall99
asked on
Return only first numeric digits in a seperate column
Hi,
I would like to return only the first two numeric digits of a column. If the first two characters are alpha-numeric, I do not want the value returned at all. Is this possible? I attached an example with expected results.
Book-1.xlsx
I would like to return only the first two numeric digits of a column. If the first two characters are alpha-numeric, I do not want the value returned at all. Is this possible? I attached an example with expected results.
Book-1.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could create a Pivot Table of unique items from the data and then select only those values that you want to include. However, this would show the whole value rather than just the first two digits.
How about some tiny VBA code ?
See example workbook attached ...
example.xlsm
Public Function FirstDigit(str As String) As Variant
Dim c As String
Dim i As Integer
For i = 1 To Len(str)
c = Mid(str, i, 1)
If InStr(1, "0123456789", c) > 0 Then
FirstDigit = c
Exit Function
End If
Next
' or something else as desired if there's no digit in str
FirstDigit = CVErr(xlErrNA)
End Function
See example workbook attached ...
example.xlsm
ASKER
@Subodh Tiwari (Neeraj) - when I click within the cell with the formula and click enter the result changes from the 'expected result' to the first two digits of the dataset (both alpha numeric and numeric). Can you please check this?
As I said, the suggested formula is an Array Formula which requires confirmation with Ctrl+Shift+Enter.
If a formula is entered as an array formula, you will notice that in the formula bar, the formula gets surrounded by the curly braces.
And if you don't see the curly braces in the formula bar that means the array formula hasn't been entered correctly and in that case select the formula cell --> press F2 to go in edit mode --> now hold down the Ctrl+Shift keys together and hit Enter.
If a formula is entered as an array formula, you will notice that in the formula bar, the formula gets surrounded by the curly braces.
And if you don't see the curly braces in the formula bar that means the array formula hasn't been entered correctly and in that case select the formula cell --> press F2 to go in edit mode --> now hold down the Ctrl+Shift keys together and hit Enter.
I've misread the question a bit, and now I've made up the VBA code.
The function returns the first uninterrupted group of digits in the string. If there's no digit in the strin, it returns #NA.
That's the result for some test strings:
example.xlsm
Public Function FirstDigits(str As String) As Variant
Dim c As String
Dim i As Integer
For i = 1 To Len(str)
c = Mid(str, i, 1)
If InStr(1, "0123456789", c) > 0 Then
FirstDigits = c
If i < Len(str) Then
For j = i + 1 To Len(str)
c = Mid(str, j, 1)
If InStr(1, "0123456789", c) > 0 Then
FirstDigits = FirstDigits + c
Else
Exit Function
End If
Next
End If
Exit Function
End If
Next
'rem or something else
FirstDigits = CVErr(xlErrNA)
End Function
The function returns the first uninterrupted group of digits in the string. If there's no digit in the strin, it returns #NA.
That's the result for some test strings:
example.xlsm
Based on a previous question from Wonderwall, summing a dataset on the basis of the first two characters of a string, I believe Neeraj has the correct interpretation of the question; the requirement is for the first two characters of the string, if they are numeric then use otherwise ignore.
Last correction:
(not incorporated in example).
Returns 2 digits at most, starting from what position ever.
Public Function FirstDigits(str As String) As Variant
Dim c As String
Dim i As Integer
For i = 1 To Len(str)
c = Mid(str, i, 1)
If InStr(1, "0123456789", c) > 0 Then
FirstDigits = c
If i < Len(str) Then
For j = i + 1 To Len(str)
c = Mid(str, j, 1)
If InStr(1, "0123456789", c) > 0 Then
FirstDigits = FirstDigits + c
If Len(FirstDigits) = 2 then Exit Function
Else
Exit Function
End If
Next
End If
Exit Function
End If
Next
'rem or something else
FirstDigits = CVErr(xlErrNA)
End Function
(not incorporated in example).
Returns 2 digits at most, starting from what position ever.
ASKER
Thanks for all your help! I was able to get the Array formula to work. I selected the 'Array Formula' solution because I'm not familiar with VBA and would be unable to update in the future if we needed a change.
You're welcome. Glad it worked.
first use the following formula to get to know the first occurrence of number
Open in new window
then use the following code to get the numbers in another column
Open in new window