Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create wild card string from tokens

Posted on 2014-02-25
4
Medium Priority
?
160 Views
Last Modified: 2014-02-26
excel 2010 vba

userform
textbox

What I need:

if the textbox contains more than  1 token the code works fine:
example:
claw hammer 16    becomes
%claw%hammer%16%

Problem:
If the textbox contains only one word:
HAMMER

it only becomes:
hammer%

I need:
%hammer%

Thanks
fordraiders


Dim Nf1 As String
Dim cArrSql As Variant
Dim cD As String
Dim x As Integer
cD = UserForm2.TextBox33

' how many items are in the box ?  build an array
            cArrSql = Split(cD, " ")
            
            
            
            Dim w As String
            ' new CODE FOR idata description match
           ' search through the text box requests tokens
                For x = LBound(cArrSql) To UBound(cArrSql)
                    
                        If x = UBound(cArrSql) Then
                                
                                Nf1 = Trim(Nf1 & cArrSql(x) & "%")
                        Else
                                If x = 0 Then
                                 Nf1 = Trim("%" & Nf1 & cArrSql(x) & "%")
                                Else
                                  Nf1 = Trim(Nf1 & cArrSql(x) & "%")
                                End If
                        End If
                Next


Nf1 = Nf1

MsgBox Nf1, vbCritical

Open in new window

0
Comment
Question by:fordraiders
[X]
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
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1600 total points
ID: 39888008
try this
Dim Nf1 As String
Dim cArrSql As Variant
Dim cD As String
Dim x As Integer
cD = UserForm2.TextBox33

' how many items are in the box ?  build an array
            cArrSql = Split(cD, " ")
            
            
            
            Dim w As String
If UBound(cArrSql) = 0 Then
 Nf1 = Trim("%" & cArrSql(0) & "%")
Else


            ' new CODE FOR idata description match
           ' search through the text box requests tokens
                For x = LBound(cArrSql) To UBound(cArrSql)
                    
                        If x = UBound(cArrSql) Then
                                
                                Nf1 = Trim(Nf1 & cArrSql(x) & "%")
                        Else
                                If x = 0 Then
                                 Nf1 = Trim("%" & Nf1 & cArrSql(x) & "%")
                                Else
                                  Nf1 = Trim(Nf1 & cArrSql(x) & "%")
                                End If
                        End If
                Next
End If

Nf1 = Nf1

MsgBox Nf1, vbCritical 

Open in new window

0
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 400 total points
ID: 39888033
Hi,

Why not, simply...

MsgBox "%" & Replace(Trim$(UserForm2.TextBox33), " ", "%") & "%", vbCritical

or

MsgBox "%" & Join(Split(UserForm2.TextBox33, " "), "%") & "%", vbCritical

?

BFN,

fp.
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39888782
Thanks very much..Fanpages..this is interesting single line of code.
I will use it for other purposes.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39888957
You're very welcome :)
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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