By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,510 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,510 IT Pros & Developers. It's quick & easy.

Left Function

P: 1
Hi i am having a problem tryin to read a string up to a chr(). The code looks like this:




strMonth = Left(strStartDate, chr(47))

what i want it to do is read the string from the left and stop when it gets to the chr(47) which is "/", but it gives me an error saying "type mismatch". Can someone tell me why this isn't working? Thanks
Feb 13 '07 #1
Share this Question
Share on Google+
7 Replies


ADezii
Expert 5K+
P: 8,601
Hi i am having a problem tryin to read a string up to a chr(). The code looks like this:




strMonth = Left(strStartDate, chr(47))

what i want it to do is read the string from the left and stop when it gets to the chr(47) which is "/", but it gives me an error saying "type mismatch". Can someone tell me why this isn't working? Thanks
You have the wrong Syntax for Left() and you are missing another required Function. This will work:
Expand|Select|Wrap|Line Numbers
  1. Dim strString As String, strSearchString As String
  2.  
  3. strString = "Yabbadabba/Doo"
  4. strSearchString = "/"
  5.  
  6. Debug.Print Left$(strString, InStr(strString, strSearchString) - 1)
  7.  
  8. 'Output: Yabbadabba
Feb 13 '07 #2

NeoPa
Expert Mod 15k+
P: 31,186
Another way is to say :
Expand|Select|Wrap|Line Numbers
  1. Dim strString As String, strSearchString As String
  2.  
  3. strString = "Yabbadabba/Doo"
  4. strSearchString = "/"
  5.  
  6. Debug.Print Split(strString, strSearchString)(0)
  7. Output: Yabbadabba
This also handles (automatically and without error) the case of there being no "/" character in the main string.
Feb 14 '07 #3

MMcCarthy
Expert Mod 10K+
P: 14,534
Alternatively, if you prefer to use the chr(47) adapting ADezii's code slightly

Expand|Select|Wrap|Line Numbers
  1.  
  2. strMonth = Left(strStartDate, InStr(strStartDate, chr(47)))
Feb 14 '07 #4

ADezii
Expert 5K+
P: 8,601
Alternatively, if you prefer to use the chr(47) adapting ADezii's code slightly

Expand|Select|Wrap|Line Numbers
  1.  
  2. strMonth = Left(strStartDate, InStr(strStartDate, chr(47)))
Mary:
Wouldn't that leave the Trailing Backslash?
Feb 14 '07 #5

MMcCarthy
Expert Mod 10K+
P: 14,534
Mary:
Wouldn't that leave the Trailing Backslash?
You are right of course.

Should be ..

Expand|Select|Wrap|Line Numbers
  1.  strMonth = Left(strStartDate, InStr(strStartDate, chr(47))-1)
Feb 15 '07 #6

NeoPa
Expert Mod 15k+
P: 31,186
I still think you're better advised to use Split() rather than Left() and InStr() (See post #3).
Feb 15 '07 #7

MMcCarthy
Expert Mod 10K+
P: 14,534
I still think you're better advised to use Split() rather than Left() and InStr() (See post #3).
I agree, just thought it was worth posting the correct syntax for the Left function. ;)
Feb 15 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.