Solved

Right Statement

Posted on 2001-06-22
17
142 Views
Last Modified: 2010-05-18
I would like to use the right statement and from a file name search for the first space that appears. Once the first space is matched I would like to add an integer behind that space.
Eg: test hello.txt
working from the right hand side, once the space is found I want the file to look like this: test(1) hello.txt

Please can someone help quick.
Thanks
0
Comment
Question by:mscala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +4
17 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6218547
right(strTest,instr(strTest," ")-1,) & "(1)" & mid(strTest, instr(strTest," ") + 1)

Zaphod.
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6218619
Public Function GetLastSpace(pzValue As String) As Integer
Dim x As Integer
Dim nPos As Integer
x = 1
While x > 0
    x = InStr(x, pzValue, " ", vbTextCompare)
    If x > 0 Then
        x = x + 1
        nPos = x
    End If
Wend
GetLastSpace = (nPos - 1)
End Function
0
 
LVL 6

Accepted Solution

by:
sharmon earned 100 total points
ID: 6218634
Sub Test()

  Dim strFile As String
  Dim strResult As String
  Dim lngPos As Long
 
  strFile = "test hello.txt"
 
  lngPos = InStrRev(strFile, " ")
 
  If lngPos <> 0 Then
    strResult = Mid$(strFile, 1, lngPos - 1) & _
                "(1)" & _
                Mid$(strFile, lngPos + 1)
  End If
 
  Debug.Print strResult
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:rspahitz
ID: 6219160
If you're using VB6, sharmon's code is best because it uses the new InStrRev command.
0
 
LVL 11

Expert Comment

by:arana
ID: 6219789
i think this is what you need (no if's but or thens)


Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " "))
MsgBox strtest
End Sub
0
 
LVL 11

Expert Comment

by:arana
ID: 6219803
correction:


(didnt erad the behind that space part)

Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " ") + 1)
MsgBox strtest
End Sub
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6219929
arana,

Your method would put the (1) in the position of the first space from the left, he wants the first space from the right.  Ontop of that, if he runs it with a string with no spaces in it, it will cause a runtime error.
0
 
LVL 11

Expert Comment

by:arana
ID: 6220094
correction:


(didnt erad the behind that space part)

Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " ") + 1)
MsgBox strtest
End Sub
0
 
LVL 11

Expert Comment

by:arana
ID: 6220115
well it gives the same results as the example he used!

but your are right, anyways just change every instr() to a instrrev() and you get the rightmost space.



oh and i dont know if i was the only one who noticed, but he said BEHIND the space? (that means left to the space keeping the space? or replace the space? or to the right of the spaace?)???

if you need to keep the space then sharmon code needs some modification, because it doesnt keep it!
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6220163
Yeah, if he can't figure out how to keep the space, he needs more help then this:)  Arana, If you do an inline Instr or InstrRev and the result is 0 and you try to use a string function on it, you will get a runtime error.  Just trying to point it out incase he didn't catch that, not for the silly points that do nothing for me, I'd trade all my points for some lunch about now.
0
 
LVL 11

Expert Comment

by:arana
ID: 6220216
well it gives the same results as the example he used!

but your are right, anyways just change every instr() to a instrrev() and you get the rightmost space.



oh and i dont know if i was the only one who noticed, but he said BEHIND the space? (that means left to the space keeping the space? or replace the space? or to the right of the spaace?)???

if you need to keep the space then sharmon code needs some modification, because it doesnt keep it!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6220826
This is not exactly what you want but...

Private Sub Form_Load()
Dim str As String
str = "This has_Two spaces"
MsgBox Replace(str, " ", "(1)", 1, 1) '
End Sub

0
 
LVL 6

Expert Comment

by:sharmon
ID: 6220848
To compliment Richie's routine, you could do this...

I wonder if we can get over 100 ways before he closes the question:)

Sub Test()
  Dim str As String
  str = "This has_Two spaces"
  str = StrReverse(str)
  str = Replace(str, " ", ")1(", 1, 1)
  str = StrReverse(str)
  MsgBox str
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6221782
sharmon,
i almost agree with you but, why we would use strreverse?
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6221850
I was just being silly, reversed the string so that the Replace would put it in the last space instead of the first....
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6221976
Oh!
We can talk each other until the end of days, Mr. msacala, are you there?
0
 

Author Comment

by:mscala
ID: 6223556
Thanks sharmom

thanks everyone actually. Sorry for the delay but I posted the question at the end of Friday my time. I am in South africa. That's why there was a delay, but thanks a million to everyone.

Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question