Solved

VBA Excel 'Find' Function

Posted on 2007-04-11
8
938 Views
Last Modified: 2013-12-25
Howdy

I'm attempting to match the strings in a specific column of worksheet A with strings in a specific column in worksheet B.  Each match found results in the entire row being copied into a third worksheet.  The number of entries in the column in worksheet A varies, so the loop would continue until whatever the VBA equivalent of EOF would be when readiing from a column.   I attempted to record a macro where I used the 'Find'/CTRL-F function.  It didn't record anything, so I'm looking for the VBA function that's tied to this action.

As always, any help is greatly appreciated.

Jason
0
Comment
Question by:cdromes
  • 5
  • 3
8 Comments
 
LVL 15

Expert Comment

by:dirknibleck
ID: 18890553
you just need to do:


activesheet.columns("A").find(activesheet.range("B1"))

(replace activesheet.range("B1") with whatever you are searching on)
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 18890569
The find function returns either a range or Nothing. (Nothing if it is empty)

so

while values in B

if not activesheet.column("A").find(activesheet.range("B1")) is nothing

  copy row to third sheet

end if

wend
0
 

Author Comment

by:cdromes
ID: 18890978
Normally, if I want to select an entire row prior to copying I do something like

Rows("1294:1294").Select

In this case, I don't have the row number of the item that was found, so how do I go about marking the entire row for copying?
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 18890996
row = activesheet.column("A").find(activesheet.range("B1")).row
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:cdromes
ID: 18891163
I'm getting an error at the IF statement.  Do I need to add an '.offset' or a '.value' in order to be more specific about what's being tested?

RowCount = 1
Do
    Sheets("HiltboldRaw").Select
    If ActiveSheet.Column("A").Find(Sheets("AffyTags").Range("B1")) <> " " Then
       Sheets("AffyTags").Range("B1").Offset(RowCount, 0).Row.Select
       Selection.Copy
       Sheets("Test").Select
       Rows("RowCount:RowCount").Select
       ActiveSheet.Paste
    End If
    RowCount = RowCount + 1
Loop Until IsEmpty(Sheets("AffyTags").Range("B1").Offset(RowCount, 0).Value)
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 18891346
This:
 If ActiveSheet.Column("A").Find(Sheets("AffyTags").Range("B1")) <> " "

Has to be:

 If Not (ActiveSheet.Columns("A").Find(Sheets("AffyTags").Range("B1")) is nothing)

*note Columns not Column (that was an error in my original post)
0
 

Author Comment

by:cdromes
ID: 18892101
It ran, but nothing was copied.  There are 32 matches that I know of.  Are there issues with this:

Sheets("AffyTags").Range("B1").Offset(RowCount, 0).Row.Select
Selection.Copy

?
0
 
LVL 15

Accepted Solution

by:
dirknibleck earned 50 total points
ID: 18894928
try:

rowcount= sheets("AffyTags").range("B1").end(xldown).row


for i = 1 to rowcount

    Sheets("HiltboldRaw").Select
    If Not (ActiveSheet.Columns("A").Find(Sheets("AffyTags").Range("B1")) is nothing) Then
       Sheets("AffyTags").Rows(i).Copy
       Sheets("Test").Select
       Rows("i").Select
       ActiveSheet.Paste
    End If
next i
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

16 Experts available now in Live!

Get 1:1 Help Now