EDUCATED MONKEY
Board Regular
- Joined
- Jul 17, 2011
- Messages
- 218
Object to place hyphens between inistials of name Only not just replace white spaces
please note that i do not want a hyphen between the last initial and the surname
Example G K Aalborg would become G-K Aalborg
this is an amendment to my earlier post, the reply I received maid me think that I would be better starting over, it seems that I am lacking the understand of so of the syntax so for that I apologise and hope help is still forth coming <?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
This is what I have so far
Sub SplitName2()<o></o>
'<o></o>
' SplitName2 Macro<o></o>
' Macro recorded <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-comffice:smarttags" /><st1:date Month="7" Day="25" Year="2011">25/07/2011</st1:date> by Peter Hayward<o></o>
'<o></o>
' Keyboard Shortcut: Ctrl+Shift+S<o></o>
Dim Q As Integer<o></o>
Dim k As Integer<o></o>
Dim S As String<o></o>
Dim MyChar As Variant<o></o>
Dim WordLen As Integer<o></o>
Dim Position As Integer<o></o>
Dim fwsp As Boolean<o></o>
<o></o>
Sheets("REMOVE UNWANTED ITEMS").Select<o></o>
Q = Worksheets("REMOVE UNWANTED ITEMS").Range("D1").Value 'how many rows for D1<o></o>
For k = 1 To Q<o></o>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value<o></o>
<o></o>
fwsp = True<o></o>
<o></o>
For Position = WordLen To 1 Step -1<o></o>
<o></o>
MyChar = Mid(S, Position, 1)<o></o>
<o></o>
Select Case MyChar.Value<o></o>
<o></o>
Case Is <> ""<o></o>
Next Position<o></o>
Case Is = ""<o></o>
fwsp = False<o></o>
Next Position<o></o>
Case Is = ""<o></o>
If fwsp = False Then<o></o>
S = Replace(S, " ", "-")<o></o>
Next Position<o></o>
End If<o></o>
<o></o>
End Select<o></o>
Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 7) = S<o></o>
Next k<o></o>
S<o></o>
End Sub
<o></o>
I am now getting a next with out for error, which occurs in either if the Next Position ifcomment out the first one, it is probably due to the fact there are in side the case statement /loop can you tell how I rearrange the code to get over this problem also if you think I am tackling this problem wrongly I would rather be pointed in the right direction even if it mean starting over as I need a clear understanding of what and why I am doing things so I am able to maintain these sub later if needed as I can not keep relying on you good guy to get me out of the whole
<o></o>
please note that i do not want a hyphen between the last initial and the surname
Example G K Aalborg would become G-K Aalborg
this is an amendment to my earlier post, the reply I received maid me think that I would be better starting over, it seems that I am lacking the understand of so of the syntax so for that I apologise and hope help is still forth coming <?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
This is what I have so far
Sub SplitName2()<o></o>
'<o></o>
' SplitName2 Macro<o></o>
' Macro recorded <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-comffice:smarttags" /><st1:date Month="7" Day="25" Year="2011">25/07/2011</st1:date> by Peter Hayward<o></o>
'<o></o>
' Keyboard Shortcut: Ctrl+Shift+S<o></o>
Dim Q As Integer<o></o>
Dim k As Integer<o></o>
Dim S As String<o></o>
Dim MyChar As Variant<o></o>
Dim WordLen As Integer<o></o>
Dim Position As Integer<o></o>
Dim fwsp As Boolean<o></o>
<o></o>
Sheets("REMOVE UNWANTED ITEMS").Select<o></o>
Q = Worksheets("REMOVE UNWANTED ITEMS").Range("D1").Value 'how many rows for D1<o></o>
For k = 1 To Q<o></o>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value<o></o>
<o></o>
fwsp = True<o></o>
<o></o>
For Position = WordLen To 1 Step -1<o></o>
<o></o>
MyChar = Mid(S, Position, 1)<o></o>
<o></o>
Select Case MyChar.Value<o></o>
<o></o>
Case Is <> ""<o></o>
Next Position<o></o>
Case Is = ""<o></o>
fwsp = False<o></o>
Next Position<o></o>
Case Is = ""<o></o>
If fwsp = False Then<o></o>
S = Replace(S, " ", "-")<o></o>
Next Position<o></o>
End If<o></o>
<o></o>
End Select<o></o>
Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 7) = S<o></o>
Next k<o></o>
S<o></o>
End Sub
<o></o>
I am now getting a next with out for error, which occurs in either if the Next Position ifcomment out the first one, it is probably due to the fact there are in side the case statement /loop can you tell how I rearrange the code to get over this problem also if you think I am tackling this problem wrongly I would rather be pointed in the right direction even if it mean starting over as I need a clear understanding of what and why I am doing things so I am able to maintain these sub later if needed as I can not keep relying on you good guy to get me out of the whole
<o></o>
Last edited: