Solved

create wild card string from tokens

Posted on 2014-02-25
4
152 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

707 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

12 Experts available now in Live!

Get 1:1 Help Now