Extract a string of variable length and position from a cell getting #VALUE!

rsd007

New Member
Joined
Oct 24, 2022
Messages
31
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I am pulling values after special character in a string. In some case I will get the value but if special character is not in the string will get Error #VALUE!

I am using Formula =MID(B1,SEARCH("@",B1)+1,8) in Cell D1

1672698977297.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
add an IFERROR
=IFERROR(MID(B1,SEARCH("@",B1)+1,8),"")

that will replace the value error with a blank

not sure it needs to be an array formula - {}
as you are only searching a cell for a character
 
Upvote 0
Thankyou for the reply,
If the length of number changes from 8 digit to 5 it pulls alphabet or whatever is after numbers
 
Upvote 0
yes it will , as it would have in the original formula
as the string is set to 8 +1 so 9

Will there only ever be 5 or 8 digits

but also you have spaces after the @ so gets a bit complicated

can you provide a few more possible examples

I have a complicated way to extract just the information between the @ and a 2nd Space
but i'm not happy with the solution
its late now in UK - so I may have a look into other possible solutions......

hopefully other members may help here

but here is the really messy formula, and may not work for all possible examples you may have
for example if there is

Excel Formula:
abc @       1234 5

it will not work - as to many spaces after the @

as i say - thinking about it - further - this is not good - so maybe IGNORE

reported , in case needs a new thread as new question


=IFERROR(MID(A1,SEARCH("@",A1,1)+1,SEARCH(" ",A1,SEARCH("@",A1,1)+2)-SEARCH("@",A1,1)),"")
may not work for all possible combinations


Book10
ABC
112aaaasssss aaaaa @ 34567 xxxx34567
21 aaaaa @ 34 xxxx34
312assss aaaaa @ 34567456789 xxxx34567456789
412aaaasssss aaaaa @345xxxxxx67bbbbbbbgggg xxxx345xxxxxx67bbbbbbbgggg
512aaaasssss aaaaa 34567 xxxx 
612aaaasssss aaaaa @abc xxxxabc
Sheet2
Cell Formulas
RangeFormula
C1:C6C1=IFERROR(MID(A1,SEARCH("@",A1,1)+1,SEARCH(" ",A1,SEARCH("@",A1,1)+2)-SEARCH("@",A1,1)),"")
 
Last edited:
Upvote 0
add an IFERROR
=IFERROR(MID(B1,SEARCH("@",B1)+1,8),"")
You could also do it this way...

=MID(B1,FIND("@",B1&"@")+1,8)

As for the follow up question about 5 or 8 characters... your request for more examples is what is needed.
 
Upvote 0
Thankyou for the Help,

Some of the entry are like.
Have text after numbers with no gaps
1672725412847.png
 
Upvote 0
Put this behind a code module.
Then you can use it as a normal function as:

Excel Formula:
=jec(A1)


VBA Code:
Function jec(xStr As String) As Variant
 With CreateObject("vbscript.regexp")
   .Pattern = "(.*@\s?)(.\d+)(.*)"
   If .test(xStr) Then jec = .Replace(xStr, "$2") Else jec = "no match"
 End With
End Function
 
Upvote 0
my solution wont work - i will need to revisit

BUT you dont have anything where after the @ , you have more than 1 space - never going to happen ?

you have shown all possible variations now .... from post #1 and post #6
 
Upvote 0
as mentioned before - this is a messy formula

=IFERROR(MID(A1,SEARCH("@",A1,1)+1,IFERROR(SEARCH(" ",A1,SEARCH("@",A1,1)+2)-SEARCH("@",A1,1),LEN(A1)-SEARCH("@",A1,1))),"")

so we are looking to see , there is an @ in the formula - if not , then the IFERROR will return a space

then we are extracting the value after the @
using a MID ()
Mid is made up of - Cell, startnumber and number of characters
SEARCH("@",A1,1)+1
we find the @ and then add 1 - so we get the start number
Now we need to know how much to extract
we are looking for a space after the @ - but not if immediately after the @ - like "@ "
we search for a space after 2 characters of the @ - so a 2nd space SEARCH(" ",A1,SEARCH("@",A1,1)+2)
Now we have that position of the 2nd space
then back to find the position of the @
we can subtract to get the length form the @ to the space
BUT if there is no 2nd space after the @ - such as "@ 123"
then we get an error - because no 2nd space
in which case we just need to find the length of the cell text and subtract the position of the @ to get the characters to extract

I'm sure there must be a simple way to do this

you will get a space in the result if a space after the @ , which you show in your examples

=IFERROR(MID(A1,SEARCH("@",A1,1)+1,IFERROR(SEARCH(" ",A1,SEARCH("@",A1,1)+2)-SEARCH("@",A1,1),LEN(A1)-SEARCH("@",A1,1))),"")

to remove any spaces in the result leading
use
TRIM()





Book10
ABC
112345678 @ 12345 678912345
212345@6789 xxxx6789
312assss aaaaa @ 34567456789 xxxx34567456789
412aaaasssss aaaaa @345xxxxxx67bbbbbbbgggg xxxx345xxxxxx67bbbbbbbgggg
512aaaasssss aaaaa 34567 xxxx 
612aaaasssss aaaaa @abc xxxxabc
7123 cxxxx @123123
8@123123
9123 
10aaa2345 
11@aaaa
Sheet2
Cell Formulas
RangeFormula
C1:C11C1=IFERROR(MID(A1,SEARCH("@",A1,1)+1,IFERROR(SEARCH(" ",A1,SEARCH("@",A1,1)+2)-SEARCH("@",A1,1),LEN(A1)-SEARCH("@",A1,1))),"")
 
Upvote 0
Thankyou for the Help,

Some of the entry are like.
Have text after numbers with no gaps
View attachment 81942
You really should show the expected result for these, so we don't have to guess. For example, in your first message you show an expected result with an "X" inside the number, so for the second example above, I assume the result would be at least A2346, but what about the lower case letters... are they to be included or excluded? And, if excluded, is that because they are not upper case? Now, what about the last example above... is the GRR to be included or not? If not included, why?
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top