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.
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)
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.