+ Reply to Thread
Results 1 to 5 of 5

search for partial match

  1. #1
    Registered User
    Join Date
    10-04-2006
    Posts
    3

    search for partial match

    Hi,
    Is it possible to create a VBA code to search for partial match?
    The logic is
    inputbox=s
    find s (where s may be exactly match or not)
    if yes, activeCell.selection
    if not, loop to next worksheet

  2. #2
    Forum Contributor starryknight64's Avatar
    Join Date
    09-27-2006
    Location
    Missouri
    MS-Off Ver
    2003 (Work) & 2007 (Home)
    Posts
    193
    See if this helps getting you started...

    Please Login or Register  to view this content.
    starryknight64

  3. #3
    Registered User
    Join Date
    10-04-2006
    Posts
    3

    Cool need steering

    Dear starryknight64,
    thank you for your quick respond.

    For I'm new in VBA (i've learnt it just for two months). I'll try to work it out. Steering is still needed. Could someone give me more advice?



    bbmh

  4. #4
    Forum Contributor starryknight64's Avatar
    Join Date
    09-27-2006
    Location
    Missouri
    MS-Off Ver
    2003 (Work) & 2007 (Home)
    Posts
    193
    Okay, I'll go through my code step-by-step.

    Sub pmatch()
    - This is a function declaration. Simply call pmatch() to run it.

    Dim a As String
    Dim b As Boolean
    Dim c As String

    - These are variable declarations. In VB 6.0, this is unneeded for most situations, but its good to have them here anyway.

    a = UCase(InputBox("Type in something...", , "fdhr436e7aaerbobaj113ngbetji4531"))
    c = UCase(InputBox("Type in what you are looking for...", , "bob"))
    b = False

    - What's happening here is I am populating the three variables with usable information. "a" contains a string, could be anything. "c" contains the string we are looking for inside of "a". "b" simply tells us if the string inside "c" was found inside "a". Seeing as how "b" is false, it hasn't been found yet. UCase takes the inputted string, and turns each character uppercase if not already done so.

    EXAMPLE:

    Please Login or Register  to view this content.
    For i = 0 To Len(a)
    - This is a "For" loop. What this means is "Start at i=0, increment i after each loop until i=Len(a)". Len(a) is simply the number of characters inside the string "a". Say, for instance, a="george". Then, Len(a) would equal 6 since there are 6 characters in the word "george".

    If Left(Right(a, Len(a) - i), Len(c)) = c Then
    - This is the hard part. Since there is no way to tell the program to look at character 2, 3, and 4 of any particular string, I had to use the "Right" and "Left" functions. "Left" and "Right" both have two inputs: a string, and an integer (we'll call the integer "num" in this case). "Left" means take the inputted string, and keep "num" left-most characters in the string. Oppositely, "Right" means take the inputted string, and keep "num" right-most characters in the string. An example is shown below. This "If" statement is keeping "Len(a) - i" characters from the right, which changes as the loop progresses, and then, from that string, keeps 3 characters from the left and checks to see if this string equals c. If it does then we execute the following lines of code, otherwise, we do not.

    EXAMPLE:

    Please Login or Register  to view this content.
    MsgBox ("A partial match was found.")
    b = True
    i = Len(a)

    - These three lines of code are what is executed if the string inside c is found. "i" is set to Len(a) simply so that the "for" loop with end.

    End If
    - This means that this is the end of the multi-line "If" statement. If this "If" statement was just one line (like the example below), then "End If" is unneeded.

    EXAMPLE:

    Please Login or Register  to view this content.
    Next i
    - This is the end of the "For" loop. All this means is increment i by 1 after executing after each loop (until i=Len(a) of course).

    If b = False Then MsgBox ("A partial match was not found.")
    - If b is still false after that for loop, then "c" was not found in "a".

    End Sub
    - This is the end of the pmatch function.

    I hope this helps!

  5. #5
    Forum Contributor starryknight64's Avatar
    Join Date
    09-27-2006
    Location
    Missouri
    MS-Off Ver
    2003 (Work) & 2007 (Home)
    Posts
    193
    Sorry about so much to take in! I just discovered the wonderful function called "mid". I have integrated it in the code below. This gets rid of that confusing Left/Right function call.

    Here's what mid does. It takes a string and two numbers. The string is any combination of characters. The first number is the starting point in the string. The second number is how many characters, beginning at the starting point, do we want to keep.

    EXAMPLE:

    Please Login or Register  to view this content.
    NEW CODE:
    (I only changed the confusing "Left/Right" if statement and changed the for loop to start at i=1 instead of i=0.)
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1