Macro: How to find & replace periods if they are preceded by characters

halinc

New Member
Joined
Nov 7, 2013
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have a set of data that I am splitting with a macro based on period placement. For example, this:
1. Joe Smith 2. Mary White 3. Ted Jones

is split into:
Joe Smith
Mary White
Ted Jones



My macro works fine except when someone has a period in their name, which creates a situation such as:
1. Joe Smith 2. J.R. Smith 3. Mary White

split into:
Joe Smith
J
R
Smith
Mary White



Is there a way I can have my macro search for periods that are preceded by characters (rather than numbers)? And then replace those periods with a space or some other character?

Thanks for any help!
Hal
 
Actually, I think I got it.. looks like the code was using "&" to denote where to parse? So I replaced with #, and it seems to work fine. Thank you both so much for all the help!! :)
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Actually, I think I got it.. looks like the code was using "&" to denote where to parse? So I replaced with #, and it seems to work fine. Thank you both so much for all the help!! :)
Here is my code modified so that you never have to worry about what characters may be embedded within the text...
Code:
Sub RTGF2()
  Dim LastRow As Long, LastCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  With Range("B2:B" & LastRow)
    .Value = Evaluate("Char(10)&IF(NOT(ISNUMBER(-LEFT(B2:B" & LastRow & "))),""."","""")&B2:B" & LastRow)
    .Replace vbLf & "*" & ".", Chr$(1), xlPart
    .TextToColumns Range("B2"), xlDelimited, , , False, False, False, False, True, Chr$(1)
    .Delete xlShiftToLeft
  End With
  LastCol = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, LookIn:=xlValues).Column
  Range("C1").Resize(, LastCol - 2).Value = Range("B1").Value
  Columns("A").Resize(, LastCol).AutoFit
End Sub
 
Upvote 0
Solution
Thank you!
If you have time, I have one more question. It seems like I should be able to do this by modifying your code, but I'm having trouble with it. I'd like something that would look through each column and put "blank" in any blank cell (not a big deal to just manually filter for blanks and fill down, but a macro would be cleaner).

So column A would be used to set how far down each other column the macro should check, then it should go through the other columns and put "blank" in the empty cells (B4, C5, D2, E5):

[TABLE="class: grid, width: 500"]
<colgroup><col span="5"></colgroup><tbody>[TR]
[TD="align: center"]Case ID
[/TD]
[TD="align: center"]Name[/TD]
[TD="align: center"]Role[/TD]
[TD="align: center"]Emp ID[/TD]
[TD="align: center"]Practice[/TD]
[/TR]
[TR]
[TD]456
[/TD]
[TD]Joe[/TD]
[TD]Mgr[/TD]
[TD][/TD]
[TD]East[/TD]
[/TR]
[TR]
[TD]8789[/TD]
[TD]1. Ted
2. Frank[/TD]
[TD]1. Emp
2. Mgr
[/TD]
[TD]1.
2. 45678[/TD]
[TD]1. West
2. East
[/TD]
[/TR]
[TR]
[TD]421[/TD]
[TD][/TD]
[TD]Emp
[/TD]
[TD]87546[/TD]
[TD]East
[/TD]
[/TR]
[TR]
[TD]35[/TD]
[TD]Sue[/TD]
[TD][/TD]
[TD]43215[/TD]
[TD]West[/TD]
[/TR]
[TR]
[TD]1548[/TD]
[TD]1. Mike
2. Hal
3. Lisa[/TD]
[TD]1. Emp
2.
3 . Mgr[/TD]
[TD]1.
2. 11234
3. 45687[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Thank you!
If you have time, I have one more question. It seems like I should be able to do this by modifying your code, but I'm having trouble with it. I'd like something that would look through each column and put "blank" in any blank cell (not a big deal to just manually filter for blanks and fill down, but a macro would be cleaner).

So column A would be used to set how far down each other column the macro should check, then it should go through the other columns and put "blank" in the empty cells (B4, C5, D2, E5):

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]Case ID
[/TD]
[TD="align: center"]Name[/TD]
[TD="align: center"]Role[/TD]
[TD="align: center"]Emp ID[/TD]
[TD="align: center"]Practice[/TD]
[/TR]
[TR]
[TD]456[/TD]
[TD]Joe[/TD]
[TD]Mgr[/TD]
[TD][/TD]
[TD]East[/TD]
[/TR]
[TR]
[TD]8789[/TD]
[TD]1. Ted
2. Frank[/TD]
[TD]1. Emp
2. Mgr[/TD]
[TD]1.
2. 45678[/TD]
[TD]1. West
2. East[/TD]
[/TR]
[TR]
[TD]421[/TD]
[TD][/TD]
[TD]Emp[/TD]
[TD]87546[/TD]
[TD]East[/TD]
[/TR]
[TR]
[TD]35[/TD]
[TD]Sue[/TD]
[TD][/TD]
[TD]43215[/TD]
[TD]West[/TD]
[/TR]
[TR]
[TD]1548[/TD]
[TD]1. Mike
2. Hal
3. Lisa[/TD]
[TD]1. Emp
2.
3 . Mgr[/TD]
[TD]1.
2. 11234
3. 45687
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Not sure whose code you are thinking of embedding this in (when you have more than one contributor, you should mention who you are thanking or responding to), but the following code line should work in anyone's code...

Range("B1:E" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlBlanks).Value = "blank"

Note: If you were referring to my macro, then you can replace what I highlighted in red with LastRow as I calculated that value earlier in my code and stored it in the LastRow variable.
 
Last edited:
Upvote 0
Rick--that works great, thank you!

And many thanks to both you and VBA Geek!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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