PDA

View Full Version : What method do I use?



mshbhwn98
09-12-2014, 03:53 AM
Hi,
What method would I use to apply a formula to a vba variable?

I have a string variable, let's call it str which I want to take the left(str,find(" ",str)-1)

Thank you

gmayor
09-12-2014, 04:04 AM
The vba equivalent (if that is what you mean) is:

Dim str As String
str = "This is a test"
str = Left(str, InStr(1, str, Chr(32)) - 1)

This will give you the string up to the first space i.e. 'This'

Alternatively you could use

Dim str As String
Dim vStr As Variant
str = "This is a test"
vStr = Split(str, Chr(32))
str = vStr(0)

mshbhwn98
09-12-2014, 04:20 AM
That is perfect. Thank you for your help. I will look up both methods to get a better understanding.

Kenneth Hobs
09-12-2014, 05:41 AM
A variant of Graham's array method to return the first element:

Sub ken() Dim str As String
str = "This is a test"
MsgBox Split(str, " ")(0)
End Sub

snb
09-12-2014, 07:24 AM
Since the space is the default words delimiter


Sub M_snb()
MsgBox Split("This is a test")(0)
End Sub