Excel/VBA 2010 - searching for wild cards

Closed
whitepanther - Jul 3, 2011 at 09:52 PM
 whitepanther - Jul 10, 2011 at 10:13 PM
Hello,

I'm wondering if it is possible to write a code that will search for wild cards. For example: we have a pattern of numbers that come up in the format 111-11-111. Is there a way to write a code that will look for ???-??-???

Also, another aspect is I'm wondering if we can search for single words. In this case different names which occur in a cell by itself without any other characters. What is happening is we have names and addresses on our spreadsheet but if there is more than one person addressed then the name of the second person appears in the row below in a cell by itself. We want to identify and delete the row with the name by itself. Here's an example of the text:

MR & MRS C & S Johnson
PO BOX 1000
WANAGNUI 1342

MR R PEARCE & MS J
SMITH
40C TRIDENT PLACE
OAKURA 4314

MR J DUNCAN & MS J
JONES
9 BULL AVENUE
KILBIRNIE
WELLINGTON 6012
Related:

3 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Jul 3, 2011 at 10:00 PM
if you use FIND in vba, you can use the wild card of * or ? as case may be

I think that answers both of your questions
0
Hi - thanks for the advice. The idea is right but the Find Function in VBA only seems to search through the modules in VBA. I'm wanting to record/write a macro to do the wild card search in my Excel spreadsheet.

Actually think I just figured the first part out. I went to Find - Options and changed Look In to Values then used the wild card ***-**-*** and that works! (thanks for the tip to use * as the wildcard).

Still can't quite figure out the second thing I'm trying to do though. I've noticed that the family account number appears in the column after the person it's address to. So for example in the records that appear correctly the record appears like this:

MR & MRS C & S Johnson AB12345
PO BOX 1000
WANAGNUI 1342

With the families address details in column B and the account number in Column C.

When there is an extra name in the line it appears like this:

MR J DUNCAN & MS J
JONES AB12365
9 BULL AVENUE
KILBIRNIE
WELLINGTON 6012

Same columns but the account number appears on the row below - along with the second persons name.

I can write a macro that goes from record to record but what I'm wondering is is there a way to get it to look at the row it's on and check to see if there is data in column C of that row; and if there isn't go down one cell cut and paste the account number to the top line then delete that second row (so using the example - Cut AB12365 paste it so it's on the line above next to Ms J then delete the row with Jones in it).

Hope that makes sense! Any ideas appreciated
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Jul 5, 2011 at 06:44 AM
You find ***-**-*** is problematic. I think you should use "?" instead of "*" as ? is place holder of one character and * is saying there can be any thing of any length

The FIND function does look at the sheet and give you the cell where it finds the match or returns NOTHING if no match is found. Below is a function that I have been using to location a row or a column within the defined range to look up a value. This function will return 0 if no match is found or will return the row number (default) or the column number (if bFindRow =false).

You can use the function to locate the row. Once row is located than you can see whats in column C of that row and do the needful


Public Function getItemLocation(sLookFor As String, _
                                rngSearch As Range, _
                                Optional bFullString As Boolean = True, _
                                Optional bLastOccurance As Boolean = True, _
                                Optional bFindRow As Boolean = True) As Long
                                
   'find the first/last row/column  within a range for a specific string
   
   Dim Cell             As Range
   Dim iLookAt          As Integer
   Dim iSearchDir       As Integer
   Dim iSearchOdr       As Integer
      
   If (bFullString) _
   Then
      iLookAt = xlWhole
   Else
      iLookAt = xlPart
   End If
   If (bLastOccurance) _
   Then
      iSearchDir = xlPrevious
   Else
      iSearchDir = xlNext
   End If
   If Not (bFindRow) _
   Then
      iSearchOdr = xlByColumns
   Else
      iSearchOdr = xlByRows
   End If
      
   With rngSearch
      If (bLastOccurance) _
      Then
         Set Cell = .Find(sLookFor, .Cells(1, 1), xlValues, iLookAt, iSearchOdr, iSearchDir)
      Else
         Set Cell = .Find(sLookFor, .Cells(.Rows.Count, .Columns.Count), xlValues, iLookAt, iSearchOdr, iSearchDir)
      End If
   End With
      
   If Cell Is Nothing Then
      getItemLocation = 0
   ElseIf Not (bFindRow) _
   Then
      getItemLocation = Cell.Column
   Else
      getItemLocation = Cell.Row
   End If
   Set Cell = Nothing

End Function
0
Thank you for that... I'm sorry but I really am very much a novice at this. I copied the coding you gave above into a module and tried to step through it to get an idea of how it works so I can try figure out what I need to adapt but I can't even get it to run!

Are you able to elaborate your example for me a bit more?

I'm afraid I'm also not sure how to write the second part once the specified row is found i.e. how to tell it to look at column C of that row to determine if there is something in that cell or not - and what action to take as a consequence.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Jul 5, 2011 at 06:55 PM
Thats a function. You cannot directly step through. You need to make a call to this function to step thru

some thing like this

sub test()
'last row with any data
lrow =getitemlocation("*", sheets("Sheet1").cells)
if (sheets("Sheet1").cells(lrow, "c") <> vbnullstring) then msgbox "there is data"

'last row with some data in column 2
lrow =getitemlocation("*", sheets("Sheet1").columns(2))
if (sheets("Sheet1").cells(lrow, "c") <> vbnullstring) then msgbox "there is data"

'first row with some data in column 1
lrow =getitemlocation("*", sheets("Sheet1").columns(1),,false)
if (sheets("Sheet1").cells(lrow, "c") <> vbnullstring) then msgbox "there is data"

end sub

It is hard to see your data here because of loss of format. Could you post a sample workbook at some share site with some sample data and post back link here
0
Hi again, just been looking at this with my colleague and yeah - we've got the search part sorted it really is just the second part that's got us stuck. We've found that if we select the first 4 cells in the row in concern and use Go To Special to look for Blank cells it will come up with an message/error if there are no blank cells. We thought maybe we could use an on error resume next command (?) to keep the loop going through the records until it finds a blank cell.... We're just not sure how to write the code for what to do if the active cell is blank i.e. if cell is blank go down cut paste etc. etc.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Jul 5, 2011 at 08:19 PM
Not entirely sure how you have coded. May be if you can paste your code,. one can tell you more
0
Hi there, Sorry meant to come back to you on this. We took a slightly different approach in the end and I've spent the last few day perfecting the entire macro - so it's now all finished!

In the end we wrote an If statement to tell it to look at where the account number was in relation to the information in the column next to it. We could see that if the Accout number was in the correct place then there would be nothing in the row above in the next column; and if there was something then we told it to copy and paste to shift the information around i.e. move the extra name up to the correct row and merged it with the other name data, then moved the acct number up and finally deleted the excess row. We told it to keep doing that until there was no more data in the next column. Works a treat! :)


Do

If IsEmpty(ActiveCell.Offset(-1, 1)) = True Then
Selection.End(xlDown).Select

Else
ActiveCell.Select
ActiveCell.Offset(-1, 0).Select
ActiveCell.FormulaR1C1 = "=RC[-1]&"" ""&R[1]C[-1]"
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(0, -1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveCell.Offset(0, 1).Select
Selection.ClearContents

ActiveCell.Offset(1, 0).Select
Selection.Copy
ActiveCell.Offset(-1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
Selection.EntireRow.Select
Selection.EntireRow.delete

ActiveCell.Select

ActiveCell.Offset(0, 2).Select
Selection.End(xlDown).Select

End If


Loop Until IsEmpty(ActiveCell.Offset(0, 1))
0