Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2023, 11:07 PM
Dave T Dave T is offline Add spaces before and after en dashes Windows 7 64bit Add spaces before and after en dashes Office 2013
Advanced Beginner
Add spaces before and after en dashes
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default Add spaces before and after en dashes

Hello All,



I am after a macro that will overwrite that data in selected cells.

I regularly copy a heading from a pdf and paste it into column G of an Excel register.
The length and content of the data being copied is never consistent.

I have found and modified some formulas that do what I am after, but this means I need a helper column and then copy the data back as values.

I would like to be able to select one or more cells and run the macro, so it edits the data in place.

The data is initially separated by en dashes then dashes, and finally an em dash. It is not unusual to find one or more spaces before or after the en dash or none at all.
The en dashes require a single space before and after each en dash and only the en dashes (not dashes or em dashes).

Initially I was only trying to add the spaces to the first three en dashes, hence my various formulas, then I realised the later parts of the headings only have dashes and an em dash, hence my other formulas.

I have also included a Function I have modified, but even though it works, I suspect I have not changed it correctly.

Any help or suggestion appreciated.

Regards, Dave T
Attached Files
File Type: xlsm Post - Add spaces before and after dashes.xlsm (21.9 KB, 3 views)
Reply With Quote
  #2  
Old 04-24-2023, 04:54 PM
p45cal's Avatar
p45cal p45cal is offline Add spaces before and after en dashes Windows 10 Add spaces before and after en dashes Office 2019
Expert
 
Join Date: Apr 2014
Posts: 867
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

Code:
Code:
Sub blah()
Dim cll
For Each cll In Selection.Cells
  cll.Value = Application.Trim(Replace(cll.Value, "–", " – "))
Next cll
End Sub
Select the cells you want processed (they don't have to be contiguous) then run the blah macro.

Same as worksheet formula
=TRIM(SUBSTITUTE(A4,"–"," – "))
(Using SUBSTITUTE without a 3rd parameter substitutes all instances)

The APPLICATION.TRIM function used in vba is the same as the worksheet function; it not only trims spaces off the start and end of the string, it also reduces multiple spaces within the string to a single space.
The SUBSTITUTE (Replace in vba) adds a space either side of each en dash, regardless of whether there were already spaces there. The TRIM later removes all but one space.
This means if there are multple spaces elsewhere in the source string, they too will be reduced to a single space.
Reply With Quote
  #3  
Old 04-25-2023, 04:23 PM
Dave T Dave T is offline Add spaces before and after en dashes Windows 7 64bit Add spaces before and after en dashes Office 2013
Advanced Beginner
Add spaces before and after en dashes
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

Hello p45cal,

Such a frustratingly simple solution that confirms I was overthinking the problem.
I also really appreciate your detailed explanation of what your macro is doing.

I had already worked out that LTrim and RTrim remove spaces from either end of the string, but not reducing any multiple mid spaces to a single space.
I had used SUBSTITUTE in a few of my earlier attempts and it worked, but each time I re-ran the macro on the same cells it kept adding extra spaces either side of the En Dash.
I then found APPLICATION.TRIM, but it seemed wrong to be adding multiple mid spaces in one part of the code and then removing them with another part of the code.

Once again… Thanks for your help and your explanation.
Regards, Dave T
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to use dashes in IF statement merges shaggy30005 Mail Merge 1 07-14-2020 10:29 PM
Add spaces before and after en dashes Add dashes to each line of a list Tomperm Word 2 02-15-2017 05:48 PM
Add spaces before and after en dashes changing colons into dashes retrofan Word 2 07-27-2014 05:05 AM
letters are dashes pmd Word 5 03-05-2012 08:08 PM
Why word does not wrap on dashes? lyrebird Word 3 06-23-2010 01:32 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft