[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VBA - Remove all dashes in cells when dash does not have a number before and after it.

Posted on 2015-01-18
9
Medium Priority
?
525 Views
Last Modified: 2015-01-18
I have this code where i replace all dashes with an empty space.

Columns("C").Replace " - ", " ", xlPart, xlByRows, True

Open in new window


Now, I would like to replace dashes when the character before AND after are not numbers.
(Format NUMBER & DASH & NUMBER)

Ex:
If I have in the cell:
St-Mary  1234-56

Open in new window


I see 2 dashes in the same cell. But one of them as a number before and after the dash. So the macro would only replace the St-Mary to St Mary.

If for example i have in the cell:
10234 Saint-John auto parc-0234

Open in new window


In this cell, Saint-John dash would be removed as i see a letter before and after the dash, and parc-0234 dash also would be removed as i don't have a combination of a Number before and after the dash.
So it would return to: 10234 Saint John auto parc 0234.

If i have in the cell:
1234 -123456

Open in new window


Since before the dash, there is an empty space, it would remove the dash as only format Number & dash & number needs to stay the same. So the result would be: 1234 123456

I need to specify that the text in each cells of column C may have different characters length.

How can i do that.

Thanks again.
0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40556639
Add VBA reference to "Microsoft VBScript Regular Expressions 5.5" and then use the following function

Function RegexReplace(str As String) As String

    Dim regEx As New RegExp
    Dim matchPattern As String, replacePattern As String
    Dim match As Boolean
    
    matchPattern = "(\D+)(-)(\D+)?"
    replacePattern = "$1 $3"
    
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = matchPattern
    End With
        
    match = regEx.test(str)
       
    While match
        str = regEx.Replace(str, replacePattern)
        match = regEx.test(str)
    Wend
    
    RegexReplace = str

End Function

Open in new window

Replace-Example.xlsm
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40556657
I would not define the dash as an own capture group, as we don't want to keep or work with it. Further the regex shown will work only if there is no digit on either side. I.e.:
    matchPattern = "(\D+)-(.*)|(.*)-(\D+)"
    replacePattern = "$1 $3"

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40556661
It looks like it work as a formula, but i will just need to transfer that into a macro.

I need to be able to replace actual cell, and not use another cell to give me the results needed.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40556687
@Wilder1626 the function provided will happily replace the cell contents I just implemented it this way to give a demo.

eg

Range("A1").Text = RegexReplace(Range("A1").Text)

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40556689
That was one of the thing i jtested using your file, but i was getting an runtime error 424: Object required.

Range("A1").Text = RegexReplace(Range("A1").Text)

Open in new window

0
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 total points
ID: 40556696
Sorry use

Range("A2").Value = RegexReplace(Range("A2").Text)

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 40556752
Thanks for your help

I was able to tweak it to:

Dim i As Integer
Dim LastRow As Integer

LastRow = [A65536].End(xlUp).Row
 For i = LastRow To 2 Step -1

If Cells(i, 1) <> "" Then
 Cells(i, 1).Value = RegexReplace(Cells(i, 1).Text)
End If

Next i
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40556756
Cool. Just a hint

The following will find the last row and work on all versions of excel. Just replace the "A" for the column you want to use.

lastrow = Range("A" & rows.count).end(xlup).row

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40556765
Using which regex?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question