Strings & Such...

Derek_35

New Member
Joined
Apr 21, 2002
Messages
25
In cell A1 the user enters a value (ie O8, U50, etc)

Two different tasks need to be performed depending on the first letter of the string (O or U) and another task is dependent on the number following the O or U.

I cant get this to work...

Function GetRight(stringToRip)
stringToRip = Right(stringToRip, Len(stringToRip) - 1)
GetRight = stringToRip
End Function

Function GetLeft(stringToRip)
stringToRip = Left(stringToRip, (1 - Len(stringToRip)))
GetLeft = stringToRip
End Function

Sub Main()

Dim uValue As String
Dim OorU As Integer

uValue = Cells(1, 1)
If GetLeft(uValue).Value = "O" Then
OorU = GetRight(uValue).Value
' Performs tasks
End If

End Sub

Any help?
This message was edited by Derek_35 on 2002-05-12 18:13
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,

In the GetRight pass, the uValue is being given the value of the "U" or "O" and has length 1.

Also, your (1-len()) syntax is incorrect, because you would essentially be taking the rightmost 0 or negative values.

The following, without the UDF calls, should do the same as you desire.

Code:
Sub Main()

Dim uValue As String
Dim Leftstring As String
Dim RightString As Integer

uValue = Cells(1, 1)
Leftstring = Left(uValue, 1)
RightString = Right(uValue, Len(uValue) - 1)

End Sub

Then test IF LeftString = "O" THEN do something ELSE do something with LeftString = "U" END IF.

HTH,
Jay
This message was edited by Jay Petrulis on 2002-05-12 21:48
 
Upvote 0
Thanks for your reply.

I tried your code and I am getting a type mismatch error on the 'RightString = Right(uValue, Len(uValue) -1)'.

This is the same error as I have been getting...
 
Upvote 0
I cannot replicate your error.

Possibly, try to coerce the value to an integer by doing something like:

Val(Right(uValue, Len(uValue) - 1))

or

CInt(Right(uValue, Len(uValue) - 1))

Give both of these a try and report the results.

Bye,
Jay
This message was edited by Jay Petrulis on 2002-05-12 21:47
 
Upvote 0
Jay's code worked for me as well.
Do all of the values entered in Cell A1 end with an integer?
Also if you have references to other object models which use the Right or Left functions, you may need to qualify yours as a VBA runtime function like this:
Strings.Left
or
Strings.Right
Probably not the latter in this case, but I have had problems with this before.
Tom
This message was edited by TsTom on 2002-05-12 20:45
 
Upvote 0
Once again, thanks for the replies. Your help is truly appreciated.

Using the Val function worked :)
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,981
Members
448,538
Latest member
alex78

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