sub will not run

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-com:office:office" /><o:p></o:p>
<o:p></o:p>
Example G K Aalborg would become G-K Aalborg <o:p></o:p>
<o:p></o:p>
<o:p></o:p>
For some reason it fails and highlights the row of code, which I have used in other sub without and difficulty <o:p></o:p>
<o:p></o:p>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G <o:p></o:p>
<o:p></o:p>
<o:p></o:p>
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:p></o:p>
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
For some reason it fails and highlights the row of code, which I have used in other sub without and difficulty <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
S = Worksheets("REMOVE UNWANTED ITEMS").Cells(k, 6).Value 'data in column G <o:p></o:p>
You inserted that line before the variable k is defined/set in your procedure.
So at that point, k would be zero, which causes errors because there is now row zero.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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