Solved

Right Statement

Posted on 2001-06-22
17
138 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
  • 5
  • 5
  • 3
  • +4
17 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
Comment Utility
right(strTest,instr(strTest," ")-1,) & "(1)" & mid(strTest, instr(strTest," ") + 1)

Zaphod.
0
 
LVL 1

Expert Comment

by:mcbeth
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

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

Expert Comment

by:arana
Comment Utility
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 10

Expert Comment

by:arana
Comment Utility
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
Comment Utility
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 10

Expert Comment

by:arana
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:arana
Comment Utility
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
Comment Utility
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 10

Expert Comment

by:arana
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sharmon,
i almost agree with you but, why we would use strreverse?
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
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
Comment Utility
Oh!
We can talk each other until the end of days, Mr. msacala, are you there?
0
 

Author Comment

by:mscala
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now