Need VBA help to copy a portion of a text string...

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I run Excel 2007 and write code that Excel 2003 users will run.

I want to be able copy all of the right characters of a text string up to the @ symbol and place them in a variable. The problem I am encountering is that the text strings are different sizes and sometimes there are anywhere between one and seven characters to the right of the of the @ symbol. Here is an example of one of the text strings:

CAM-20016W @AT_G04

In this case, I want to place AT_G04 (Total of six characters) in a variable and recall it later.

Another example is:

GNAM-GB010W @F_D04

F_D04 has a total of five characters.

I cannot find any reference material that would tell me how to code this.

Can anyone help me with this?

Thanks,

Charles
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This should help you in what you are after. You will need error handling for when no "@" is found. Change "selection" as you require, and obviously get rid of the msgbox..

Code:
MsgBox Right(Selection, Len(Selection) - WorksheetFunction.Find("@", Selection))
 
Upvote 0
Another way:

Code:
Function RightOfAt(sInp As String) As String
    RightOfAt = Mid(sInp, InStr(sInp, "@") + 1)
End Function
 
Upvote 0
... and another:

Code:
Function RightOfAt(sInp As String) As String
    RightOfAt = Split(sInp, "@")(1)
End Function
 
Upvote 0
I think that breaks if there is no "@", pgc.
 
Upvote 0
I think that breaks if there is no "@", pgc.

You are right, shg. I assumed the string has a "@".
If it has not maybe it should return an empty string?

Like:

Code:
Function RightOfAt1(sInp As String) As String
    If InStr(sInp, "@") Then RightOfAt1 = Split(sInp, "@")(1)
End Function

Btw, your function should perhaps do the same test? If the string does not have the "@" maybe is better that the function returns the empty string, instead of the input string.
 
Upvote 0
You're right, the OP may want a null string if there's no @ rather than the whole string. Returning the whole string probably makes more sense for the "LeftOfAt" function.
 
Upvote 0
... and a simpler version of my function:

Code:
Function RightOfAt(sInp As String) As String
    RightOfAt = Split(sInp & "@", "@")(1)
End Function
 
Upvote 0
pgc01, shg and baitmaster,

Thank you for your help! I am sure that one of the examples you gave me will work. I could not see in my mind how to construct the code. You have provided me with much help.

By the way, I did not mention in my original post that the text string I will be working with always has a "@" preceeding the portion of text that I want to copy.

Thank you again!

Charles
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,515
Members
452,921
Latest member
BBQKING

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