Find and Replace or RegEx Help

nickyc

New Member
Joined
Sep 26, 2011
Messages
5
Hi guys,

I have a find and replace function that removes + smybols from a coloum of strings. How can I remove the first instance of a space (if it later contains a +, too?

E.g.

Before: [ +Test +Test]
After: [+Test +Test]

Code:
    Columns("D:D").Select
    Selection.Replace What:="+", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi

Try this

Code:
Sub Replace_Char()
Dim r As Range, cell As Range

Set r = Application.Intersect(ActiveSheet.UsedRange,ActiveSheet.Range("D:D"))

With CreateObject("vbscript.regexp")
  .Global = False
  .Pattern = "( )(.*\+)"
  For Each cell In r
    cell.Value = .Replace(cell.Value,"$2")
  Next cell
End With
r.Replace What:="+", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 
Upvote 0
Hi there,
I keep getting a type mismatch error when I have:

+Test +Test
=+Test +Test

The second data shows as <TABLE style="WIDTH: 75pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=100><COLGROUP><COL style="WIDTH: 75pt; mso-width-source: userset; mso-width-alt: 3657" width=100><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 75pt; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" height=20 width=100 align=center>#NAME?</TD></TR></TBODY></TABLE>
<TABLE style="WIDTH: 75pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=100><COLGROUP><COL style="WIDTH: 75pt; mso-width-source: userset; mso-width-alt: 3657" width=100><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 75pt; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" height=20 width=100>+Test +Test</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" height=20 align=center>#NAME? (this is </TD></TR></TBODY></TABLE>
 
Upvote 0
Hello nickyc,

Try this macro. It will replace all the "+" signs and any single space prefixes.

Code:
Sub FindAndReplace()

  Dim Cell As Range
  Dim RegExp As Object
  Dim Rng As Range
      
      Set Rng = ActiveSheet.UsedRange.Columns(4).Cells
  
      Set RegExp = CreateObject("VBScript.RegExp")
      RegExp.Global = True
      RegExp.Pattern = "\s?\+"
      
          For Each Cell In Rng
            Cell = RegExp.Replace(Cell, "")
          Next Cell
          
End Sub
 
Upvote 0
Hi there,

Still getting a mismatch error. Does it have something to do with the = sign as the String cell?
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,553
Members
452,928
Latest member
101blockchains

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