Solved

VBA Excel 'Find' Function

Posted on 2007-04-11
8
943 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

19 Experts available now in Live!

Get 1:1 Help Now