Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create wild card string from tokens

Posted on 2014-02-25
4
Medium Priority
?
162 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

824 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