Removing hyphens and capitalizing - VBA String

Hi

I am building a Macro in Excel that corrects a big list of first and second names.
The first names are located in column A and the second names in column B.
I am just about there with everything apart from the following rule:

If the first name or the second name contains a hyphen (-) then I need the code to remove this hyphen and replace it with a space but also the letter directly after the hyphen to be capitalized… for instance:


First Name            Second Name            
John-lee            Smith

…..would become

First Name            Second Name            
John Lee            Smith


I have got the following code which replaces hyphens with spaces but I need it to also capitalize as per the rule above.

Set rng = Selection
    For Each c In rng
        c.Replace What:="-", Replacement:=" ", LookAt:=xlPart, _
                  SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                  ReplaceFormat:=False
       
    Next c

If any hyphens are removed with spaces I would also like the cell colour to turn red if possible, I’d guess it would do this by checking whether it has actually made a replacement…

Thanks,
jasocke2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

starting off with your code, here are some amendments that do what you describe

cheers, teylyn
Sub test()
Set Rng = Selection
    For Each c In Rng
        If InStr(1, c.Value, "-") <> 0 Then
            c.Replace What:="-", Replacement:=" ", LookAt:=xlPart, _
                  SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                  ReplaceFormat:=False
            c.Font.ColorIndex = 3
        End If
        c.Value = WorksheetFunction.Proper(c)
       
    Next c
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyberkiwiCommented:
Try this
Sub test()

Dim dash As Long, Rng As Range, c As Range
Set Rng = Selection
For Each c In Rng
    dash = InStr(c.Value, "-")
    While dash > 0
        c.Value = Left(c.Value, dash - 1) & " " & UCase(Mid(c.Value, dash + 1, 1)) & Mid(c.Value, dash + 2)
        dash = InStr(c.Value, "-")
        c.Interior.Color = RGB(255, 0, 0)
    Wend
Next c

End Sub

Open in new window

cyberkiwiCommented:
teylyn,

Good to see you again.
Nice use of the Proper function.  I went for a different approach because I wasn't sure if "d" in "da" for names like "Eduardo da Silva" were to be left alone.  You could be right there.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Jorge PaulinoIT Pro/DeveloperCommented:
cyberkiwi,
You could also use the StrConv() function from VBA:
StrConv("some text", vbProperCase)
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hi cyberkiwi, I'm sitting in a hotel in your town. Lovely harbour views. Will be in town tomorrow, too. Catch a drink?
jasocke2Author Commented:
Both great answers thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.