Solved

create wild card string from tokens

Posted on 2014-02-25
4
157 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
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 400 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 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associateā€¦
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.
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ā€¦
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

749 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