replacing the last space in a variable with an &...

GS7CLW

Banned
Joined
Aug 10, 2010
Messages
168
I have a variable that may consist of 1 to 6 words:

Variable name = NewWords

a sample sting may be:

PHYS

PHYS ENLIST

TEST PHYS ENLIST

I am trying to figure out how to replace the last space within the variable with 2 spaces and the ampersand, so that:

TEST PHYS ENLIST

would end up being:

TEST PHYS & ENLIST

I am doing this within a macro.

Hope everyone had a great weekend!!!
cliff
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
=if(len(a1)=len(substitute(a1," ","")),a1,substitute(a1," "," & ",len(a1)-len(substitute(a1," ",""))))

EDIT: Oops, just noticed you want a VBA solution...
 
Upvote 0
InStrRev returns the last occurrence of a character in a string, so if you have "TEST PHYS ENLIST" in NewWords:-
Code:
Dim iSpace As Integer
iSpace = InStrRev(NewWords, Space(1))
If iSpace > 0 Then NewWords = Left(NewWords, iSpace) & "&" & Mid(NewWords, iSpace)
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Feb10
[COLOR="Navy"]Dim[/COLOR] MyStg [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
'[COLOR="Green"][B]MyStg = "PHYS"[/B][/COLOR]
MyStg = "TEST PHYS ENLIST"
num = InStrRev(MyStg, " ")
MyStg = IIf(num > 0, Left(MyStg, num) & "&" & Right(MyStg, Len(MyStg) - num + 1), MyStg)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Will perform the substitution on all selected cells...

Code:
Sub test()
Dim r As Range
Dim Count

For Each r In Selection
Count = Len(r.Value) - Len(WorksheetFunction.Substitute(r.Value, " ", ""))

If Count <> 0 Then r.Value = WorksheetFunction.Substitute(r.Value, " ", " & ", Count)

Next r


End Sub
 
Upvote 0
InStrRev returns the last occurrence of a character in a string, so if you have "TEST PHYS ENLIST" in NewWords:-
Code:
Dim iSpace As Integer
iSpace = InStrRev(NewWords, Space(1))
If iSpace > 0 Then NewWords = Left(NewWords, iSpace) & "&" & Mid(NewWords, iSpace)

Mr Ruddles,

I tried yours and it puts the ampersand at the very end of the strings.
but I do appreciate the help.

cliff
 
Upvote 0
basically what I am trying to do is fill in the blank:

Code:
If InStr(c.Value, " ") > 0 Then ' test for a space
NewWords =                      ' if there is a space; replace the LAST space with an &
End If

a sample line (NewWords) could be:
PHYS ENLIST
LA
TEST PHYS ENLIST
 
Last edited:
Upvote 0
SOLVED: replacing the last space in a variable with an &...

Mr Ruddles:

I ran:

NewWords = Trim(NewWords)

just before your code and everything works perfectly now.

THANKS!!!!
cliff
 
Upvote 0
Are you building NewWords one word at a time? Maybe the trailing space is appearing at that stage.
 
Upvote 0
basically yes:

Code:
Sub CleanUpCommentColumn()
'
' Clean Up the Comment Column Macro
' AUTHORIZED WORD LIST
' LA
' CONSULT
' INSPECT
' TEST
' PHYS
' ENLIST
' DEP IN
' TAPAS
' INTERVIEW
' SEC INT
Application.ScreenUpdating = False ' turn OFF the screen updating
'---dim the variables
Dim NewWords As String
Dim c As Range
For Each c In Range("L1:L200")
If InStr(c.Value, "LATE") > 0 Then
NewWords = "LA "
End If
If InStr(c.Value, "*PS*") > 0 Then
NewWords = NewWords & "PS "
End If
If InStr(c.Value, "PRIOR SERVICE") > 0 Then
NewWords = NewWords & "PS "
End If
If InStr(c.Value, "CONSULT") > 0 Then
NewWords = NewWords & "CONSULT "
End If
If InStr(c.Value, "TAPAS") > 0 Then
NewWords = NewWords & "TAPAS "
End If
If InStr(c.Value, "INSPECT") > 0 Then
NewWords = NewWords & "INSPECT "
End If
If InStr(c.Value, "ASVAB") > 0 Then
NewWords = NewWords & "TEST "
End If
If InStr(c.Value, "TEST") > 0 Then
NewWords = NewWords & "TEST "
End If
If InStr(c.Value, "PHYS") > 0 Then
NewWords = NewWords & "PHYS "
End If
If InStr(c.Value, "ENLIST") > 0 Then
NewWords = NewWords & "ENLIST "
End If
If InStr(c.Value, "DEP") > 0 Then
NewWords = NewWords & "DEP-IN "
End If
If InStr(c.Value, "INTERVIEW") > 0 Then
NewWords = NewWords & "SEC-INT "
End If
NewWords = Trim(NewWords)
Dim iSpace As Integer
iSpace = InStrRev(NewWords, Space(1))
If iSpace > 0 Then NewWords = Left(NewWords, iSpace) & "&" & Mid(NewWords, iSpace)
c.Value = NewWords
NewWords = ""
Next c
Columns("L:L").EntireColumn.AutoFit
Application.ScreenUpdating = True ' turn ON the screen updating
End Sub

It works perfectly!!!!!!!!!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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