?
Solved

VBA Excel 'Find' Function

Posted on 2007-04-11
8
Medium Priority
?
965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 15

Expert Comment

by:dirknibleck
ID: 18890996
row = activesheet.column("A").find(activesheet.range("B1")).row
0
 

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 200 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

800 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