Solved

Excel VBA Trim function

Posted on 2013-11-15
6
447 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
Comment Utility
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 80

Accepted Solution

by:
byundt earned 350 total points
Comment Utility
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
Comment Utility
Another way:

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


Dim FirstGroup As String

FirstGroup = Left(MyString, InStr(MyString, " ") - 1)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 45

Assisted Solution

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

Dim FirstGroup As String


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

Open in new window

0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 350 total points
Comment Utility
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
Comment Utility
Thanks everybody they were all great solutions.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now