Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with list search please.

Status
Not open for further replies.

msnook

Technical User
Jul 6, 2007
41
US
I am dealing with a list of 500 rows and 32 columns. I am trying to find one of 45 different items in any one of 4 columns. I wrote a macro that works and I have included it below. If there is a better or faster way to do this the input would be greatly appreciated. The 'List' page contains the 45 different items. The macro chooses the first item and then searches for that item on the 'Temp Page' in column J. If it finds a match it copies and moves it to the 'Transfer Data' page and then deletes the entry so that I wont get duplicates. The last little coding is to change colors of the last cell so that I can see where the entries end. The problem is that this runs through all 45 entries in column J and then I need to do the same again for columns N, Q, and T which makes it's perfomance somewhat slower than I would like. Again any assistance would be greatly appreciated and I thank everyone in advance.
-------------------------------------
For z = 1 To 45

Worksheets("List").Select
Range("A" & z).Select
MyDat2 = Range("A" & z)

If MyDat2 <> "" Then
For a = 1 To 250
Worksheets("Temp Page").Select
With Worksheets("Temp Page")
If Range("A" & a) <> "" Then

If Range("J" & a) = MyDat2 Then
Range("A" & a, "T" & a).Copy
Sheets("Transfer Data").Select
Range("A" & b).Select
ActiveSheet.Paste
b = b + 1
Sheets("Temp Page").Select
Range("A" & a, "L" & a).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp

End If
End If
End With
Next a
End If
Next z

Worksheets("Transfer Data").Select
Range("A" & b - 1).Select
With Selection.Interior
.ColorIndex = 11
.Pattern = xlSolid
End With
 
Well, I think using the Range.Find method will be more elegant and might be faster.
Excel VBA said:
Finds specific information in a range, and returns a Range object that represents the first cell where that information is found. Returns Nothing if no match is found. Doesn’t affect the selection or the active cell.

For information about using the Find worksheet function in Visual Basic, see Using Worksheet Functions in Visual Basic.

expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
expression Required. An expression that returns a Range object.

What Required Variant. The data to search for. Can be a string or any Microsoft Excel data type.

After Optional Variant. The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface. Note that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn’t searched until the method wraps back around to this cell. If you don’t specify this argument, the search starts after the cell in the upper-left corner of the range.

LookIn Optional Variant. The type of information.

LookAt Optional Variant. Can be one of the following XlLookAt constants: xlWhole or xlPart.

SearchOrder Optional Variant. Can be one of the following XlSearchOrder constants: xlByRows or xlByColumns.

SearchDirection Optional XlSearchDirection. The search direction.

XlSearchDirection can be one of these XlSearchDirection constants.
xlNext default
xlPrevious

MatchCase Optional Variant. True to make the search case sensitive. The default value is False.

MatchByte Optional Variant. Used only if you’ve selected or installed double-byte language support. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.

SearchFormat Optional Variant. The search format.

Remarks
The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

You can use the FindNext and FindPrevious methods to repeat the search.

In its simplest form, you just give it the range (say, Application.union([j1:j500],[n1:n500],[q1:q500],[t1:t500])) and just supply the FindWhat argument. Then use the FindNext:
Continues a search that was begun with the Find method. Finds the next cell that matches those same conditions and returns a Range object that represents that cell. Doesn’t affect the selection or the active cell.

expression.FindNext(After)
expression Required. An expression that returns a Range object.

After Optional Variant. The cell after which you want to search. This corresponds to the position of the active cell when a search is done from the user interface. Note that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn’t searched until the method wraps back around to this cell. If this argument isn’t specified, the search starts after the cell in the upper-left corner of the range.

Remarks
When the search reaches the end of the specified search range, it wraps around to the beginning of the range. To stop a search when this wraparound occurs, save the address of the first found cell, and then test each successive found-cell address against this saved address.

Example
This example finds all cells in the range A1:A500 that contain the value 2 and changes their values to 5.

With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top