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
Solved

Excel VBA Trim function

Posted on 2013-11-15
6
450 Views
Last Modified: 2013-11-15
I encounter this value...

GQ-000 - Regulatory Guidances

Is there some sort of function that would allow me to trim the string so I just get...
GQ-000

In other words get the 1st group of characters until the space is encountered.

I also may encounter
GQ-010_2 - Legalization
I just want the value
GQ-010_2
0
Comment
Question by:tesla764
6 Comments
 
LVL 15

Assisted Solution

by:unknown_routine
unknown_routine earned 100 total points
ID: 39651694
Dim MyString As String
MyString = "GQ-000 - Regulatory Guidances"


Dim FirstGroup As String

Dim arr

arr = Split(MyString, " ") 'split string by spaces

FirstGroup = arr(0)
0
 
LVL 81

Accepted Solution

by:
byundt earned 350 total points
ID: 39651703
Assuming that your space is ASCII 32:
Dim s As String, sInput as String
s = Left(sInput, InStr(1, sInput & " ", " ") -1)

Open in new window

0
 
LVL 15

Assisted Solution

by:unknown_routine
unknown_routine earned 100 total points
ID: 39651706
Another way:

Dim MyString As String
MyString = "GQ-000 - Regulatory Guidances"


Dim FirstGroup As String

FirstGroup = Left(MyString, InStr(MyString, " ") - 1)
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 50 total points
ID: 39651714
unknown's code can be modified like this

Dim FirstGroup As String


FirstGroup = Split(MyString, " ")(0)

Open in new window

0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 350 total points
ID: 39651722
And if you want to handle the possibility of a non-breaking space, you could use either of:
Dim s As String, sInput as String
s = Split(Replace(sInput,Chr(160)," ")," ")(0)
s = Left(sInput, InStr(1, Replace(sInput,Chr(160)," ") & " ", " ") -1)

Open in new window


I'm liking the Split method suggested by unknown_routine and MartinLiss better than my Left and InStr.
0
 

Author Comment

by:tesla764
ID: 39651736
Thanks everybody they were all great solutions.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

792 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