EDUCATED MONKEY
Board Regular
- Joined
- Jul 17, 2011
- Messages
- 218
Object to place hypens between inistials of name Only not just replace white spaces<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
Example G K Aalborg would become G-K Aalborg <o></o>
<o></o>
<o></o>
For some reason it fails and highlights the row of code, which I have used in other sub without and difficulty <o></o>
<o></o>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G <o></o>
<o></o>
<o></o>
Hope you can help
Sub SplitName()
'
' SplitName Macro
' Macro recorded 24/07/2011 by Peter Hayward
'
' Keyboard Shortcut: Ctrl+Shift+F
'
Application.ScreenUpdating = False
Dim S As String
Dim Q As Integer
Dim k As Integer
Dim ws As Integer
Dim fwsp As Boolean
Dim MyString As String
Worksheets("REMOVE UNWANTED ITEMS").Activate
Q = Worksheets("REMOVE UNWANTED ITEMS").Range("D1").Value 'how many rows
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G
For k = 1 To Q 'row iterator
fwsp = True 'is it the first white space row?
For ws = Len(S) To 1 Step -1 'char iterator backwards
MyString = Mid(S, ws, 1)
If MyString = " " And fwsp = True Then
ws = False
End If
If MyString = " " And fwsp = False Then
MyString = Replace(MyString, " ", "-")
End If
Next ws
Next k ' get next string
Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 7).Value = S ' processed data in column H
Application.ScreenUpdating = True
End Sub<o></o>
<o></o>
Example G K Aalborg would become G-K Aalborg <o></o>
<o></o>
<o></o>
For some reason it fails and highlights the row of code, which I have used in other sub without and difficulty <o></o>
<o></o>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G <o></o>
<o></o>
<o></o>
Hope you can help
Sub SplitName()
'
' SplitName Macro
' Macro recorded 24/07/2011 by Peter Hayward
'
' Keyboard Shortcut: Ctrl+Shift+F
'
Application.ScreenUpdating = False
Dim S As String
Dim Q As Integer
Dim k As Integer
Dim ws As Integer
Dim fwsp As Boolean
Dim MyString As String
Worksheets("REMOVE UNWANTED ITEMS").Activate
Q = Worksheets("REMOVE UNWANTED ITEMS").Range("D1").Value 'how many rows
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G
For k = 1 To Q 'row iterator
fwsp = True 'is it the first white space row?
For ws = Len(S) To 1 Step -1 'char iterator backwards
MyString = Mid(S, ws, 1)
If MyString = " " And fwsp = True Then
ws = False
End If
If MyString = " " And fwsp = False Then
MyString = Replace(MyString, " ", "-")
End If
Next ws
Next k ' get next string
Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 7).Value = S ' processed data in column H
Application.ScreenUpdating = True
End Sub<o></o>