How do I split String

hill0093

Board Regular
Joined
Dec 4, 2011
Messages
163
How do I code
Code:
Dim stg As String 
Dim lgt As Integer 
Dim num As Integer
lgt = Length(stg)
If First Character of stg is 'p' Then num = last (lgt-1) Characters of stg
I don't know how to get Record New Macro to tell me.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
In Excel 2003 I have a String.
If the first character equals p then I want to
take the rest of the string as an integer or long
 
Upvote 0
Try

Dim stg As String
Dim lgt As Integer
Dim num As Long
If UCase(Left(stg, 1)) = "P" Then num = CLng(Right(stg, lgt - 1))
 
Upvote 0
Sorry, I thought you already had it...

This
lgt = Length(stg)
should be changed to
lgt = Len(stg)
 
Upvote 0
Glad to help, thanks for the feedback.

Note, the use of Ucase was to make the code NON Case Sensitive (it will work with p or P)
If you WANT it to be case sensitive (work with P, but NOT with p) then just remove the Ucase function.
 
Upvote 0
I know this is a bit old & resolved but I just stumbled across it so I'm adding a few comments.

Unless you need the length of the string stg for other purpose anyway, you don't need to calculate that.

I always use Long (not Integer) as Integer values have to be converted to Long anyway for vba to work with them (Long is shorter to write too ;))

Since num is declared as Long (or Integer) there should be no need to force conversion with CLng()

It is also possible to avoid extracting Left(stg,1)

I'm not suggesting anything wrong with the existing code, but here is another way to attack the problem.
It has the same case-sensitivity issues as described by Jonmo1.
Code:
Dim stg As String
Dim num As Long


If UCase(stg) Like "P*" Then num = Mid(stg, 2)
 
Upvote 0

Forum statistics

Threads
1,203,399
Messages
6,055,174
Members
444,767
Latest member
bryandaniel5

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