• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

How to Remove Spaces before and after but NOT within a string|General Excel Questions & Answers|Excel Forum|My Online Training Hub

You are here: Home / How to Remove Spaces before and after but NOT within a string|General Excel Questions & Answers|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumGeneral Excel Questions & Answe…How to Remove Spaces before and aft…
sp_PrintTopic sp_TopicIcon
How to Remove Spaces before and after but NOT within a string
Avatar
Rosemary Ventura

New Member
Members
Level 0
Forum Posts: 2
Member Since:
November 13, 2021
sp_UserOfflineSmall Offline
1
November 18, 2021 - 6:40 am
sp_Permalink sp_Print

Help!

How do I remove LEADING and TRAILING spaces in a cell, but NOT the spaces between or within the string?

I've tried everything I know of to figure this out, but can't find a solution.  We have part numbers on our system that intentionally look like this:

BA48282 1936  D

BA 524 X 18        N

BA 5811 1934        92

Yes, sometimes the part number will have 1 space, 2 spaces, or many spaces within the part number itself.  This is by design.  When we run reports to Excel and do something like a VLOOKUP, the parts may not always match.  The biggest reason is because sometimes the part number comes into Excel with spaces AFTER the part number, and sometimes not.

Most people use TRIM(), but that removes all the spaces within the cell which could change the actual part number.

I've tried to use this formula    =MID(A2,FIND(MID(TRIM(A2),1,1),A2),LEN(A2)), but that only removes the leading spaces.  It does KEEP the spaces within the part number, but I need to also remove the trailing spaces.

Thanks,

Rose

Avatar
Mynda Treacy
Admin
Level 10
Forum Posts: 4438
Member Since:
July 16, 2010
sp_UserOfflineSmall Offline
2
November 18, 2021 - 8:58 am
sp_Permalink sp_Print sp_EditHistory

Hi Rosemary,

I understand that the spaces in the middle of the text string can vary, but do they always have the same number of leading and trailing spaces and if so, you can specify this number in your MID formula calculation to skip/ignore.

BTW, it's always best to include a small Excel file with a sample of your real data that covers every scenario so we can get the answers to these questions without having to ask you.

Mynda

Avatar
MATTHEW STANSBERRY
Member
Members
Level 0
Forum Posts: 7
Member Since:
October 10, 2021
sp_UserOfflineSmall Offline
3
November 18, 2021 - 9:19 pm
sp_Permalink sp_Print

Hi Rosemary,

have you tried:

=RIGHT(A1, LEN(A1)-1)

=LEFT(A1, LEN(A1)-1)

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1510
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
November 19, 2021 - 1:02 pm
sp_Permalink sp_Print

Hi Rosemary,

With Power Query you can use the Text.TrimStart and Text.TrimEnd functions - see attached file

 

Text.TrimStart - PowerQuery M | Microsoft Docs

Text.TrimEnd - PowerQuery M | Microsoft Docs

 

= Text.TrimStart(Text.TrimEnd([Part Number]))

 

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Laxmi Praveen
Guest(s) 10
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
yashal minahil
Oluwadamilola Ogun
Yannik H
dectator mang
Francis Drouillard
Orlando Inocente
Jovitha Clemence
Maloxat Axmatovna
Ricardo Freitas
Marko Meglic
Forum Stats:
Groups: 3
Forums: 24
Topics: 6201
Posts: 27185

 

Member Stats:
Guest Posters: 49
Members: 31861
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.