Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Compare & Copy Specific Text Values

Posted on 2014-04-29
Medium Priority
163 Views
I have the following code that I would like to add critieria too, however I can not get it work based on what I added

``````Sub aaa()
Dim ChkSH As Worksheet
Set ChkSH = Sheets("LM")

ce = "Local Market"

Sheets("Variance").Activate
For Each ce In Range("a4:a" & Cells(Rows.Count, 1).End(xlUp).Row)
Set findit = ChkSH.Range("a4:a3000").Find(what:=ce, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If findit Is Nothing Then Range("A" & ce.Row).Copy _
ChkSH.Range("a65000").End(xlUp).Offset(1, 0)

Next ce
End Sub
``````

From the Variance worksheet I want to only compare and copy over the rows where Column B text = Local Market.  As it is written I am returning all values in Column B.
0
Question by:jmac001
[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
• 4
• 4

LVL 27

Expert Comment

ID: 40030756
Try this:
``````Sub aaa()
Dim ChkSH As Worksheet
Dim r As Row
Dim ce As String

Set ChkSH = Sheets("LM")
ce = "Local Market"

Sheets("Variance").Activate
For Each r In Sheets("Variance").Range("a4:a" & Cells(Rows.Count, 1).End(xlUp).Row)
Set findit = ChkSH.Range("a4:a3000").Find(what:=ce, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If findit Is Nothing Then Range("A" & ce.Row).Copy _
ChkSH.Range("a65000").End(xlUp).Offset(1, 0)

Next
End Sub
``````
0

Author Comment

ID: 40032052
Hi,

Receiving User-defined type not defined compile error (Dim r As Row).  Attaching a sample of the workbook vba in Module 4.

Thanks
EE-Timeline-Data-2014.04.29.xlsm
0

LVL 27

Expert Comment

ID: 40032473

``````Sub aaa()
Dim ChkSH As Worksheet
Dim r As Range
Dim ce As String

Set ChkSH = Sheets("LM")
ce = "Local Market"

Sheets("Variance").Activate
For Each r In Sheets("Variance").Range("a4:a" & Cells(Rows.Count, 1).End(xlUp).Row).Rows
Set findit = ChkSH.Range("a4:a3000").Find(what:=ce, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If findit Is Nothing Then Range("A" & r.Row).Copy _
ChkSH.Range("a65000").End(xlUp).Offset(1, 0)

Next
End Sub
``````
0

Author Comment

ID: 40032511
Still have a problem all of the records are still being copied over
0

LVL 27

Accepted Solution

ID: 40032762
This is the quickest most efficient way to do what you want, it uses autofilter.

``````Sub aaa()
Sheets("Variance").Range("A4:AC" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter 2, "Local Market"
Sheets("Variance").Range("A4:AC" & Cells(Rows.Count, 1).End(xlUp).Row).Copy Sheets("LM").Range("A65000").End(xlUp).Offset(1, 0)
Sheets("Variance").ListObjects("Var").Range.AutoFilter Field:=2
End Sub
``````
0

Author Comment

ID: 40033093
This does workk.  In your opinion, I have to consistanly update the report, it better to copy and repaste the values or do a comparison and add only the new values?
0

LVL 27

Expert Comment

ID: 40033113
It will be much more efficient and a whole lot quicker to copy and paste all the the records.
0

Author Closing Comment

ID: 40033119
Thanks for you help and the alternative solution
0

## Featured Post

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
###### Suggested Courses
Course of the Month5 days, 2 hours left to enroll