Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Problem with a macro finding cell values

I'm having an issue with the macro code below. Basically it's looking for a
value in one worksheet and then assigning that value to a variable called
"Item." Then it looks in another worksheet for the value. If it doesn't find
it it goes back to the first sheet and highlights it. The problem I'm running
into is that the same value can be in the first worksheet more than once.
It's not highlighting the first instance of the value but it is highlighting
every instance after it, which I don't want it to do. Any ideas?

With Sheets("Kits").Range("C1:C150000")
Set Rng = .Find(What:=Item, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)

If Not Rng Is Nothing Then
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
Else
With Selection.Interior
.Pattern = x1Solid
.PatternColorIndex = x1Automatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
End If
End With

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 245
Default Problem with a macro finding cell values

Try this

Sub CompareVals()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim YourRangeA As Range, YourRangeB As Range
Dim FoundCell As Range, ALastRow As Byte
Dim i As Variant

Set ws1 = ActiveWorkbook.Worksheets(1)
Set ws2 = ActiveWorkbook.Worksheets(2)
ALastRow = ws1.Cells(Cells.Rows.Count, 1).End(xlUp).Row
Set YourRangeA = ws1.Range("A1:A" & ALastRow)
Set YourRangeB = ws2.Range("A1:IV65536")


For Each i In YourRangeA
Set FoundCell = YourRangeB.Find(What:=i, LookIn:=xlValues)
With YourRangeB
.Find What:=i, LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True
If FoundCell Is Nothing Then
i.Interior.ColorIndex = 6
Else
Application.StatusBar = "Found Value " & FoundCell
End If
End With
Next
End Sub


"Dave L" wrote:

I'm having an issue with the macro code below. Basically it's looking for a
value in one worksheet and then assigning that value to a variable called
"Item." Then it looks in another worksheet for the value. If it doesn't find
it it goes back to the first sheet and highlights it. The problem I'm running
into is that the same value can be in the first worksheet more than once.
It's not highlighting the first instance of the value but it is highlighting
every instance after it, which I don't want it to do. Any ideas?

With Sheets("Kits").Range("C1:C150000")
Set Rng = .Find(What:=Item, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)

If Not Rng Is Nothing Then
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
Else
With Selection.Interior
.Pattern = x1Solid
.PatternColorIndex = x1Automatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
End If
End With

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Problem with a macro finding cell values

This works somewhat, but I'm still getting some of the same issues happening.
It seems that if the value exisits in both sheets multiple times then it
flags it as not found. Any ideas?

"Office_Novice" wrote:

my last post + A few minor revisions. This may require you to modify a few
things Sheet names and ranges ... but it should do what you want.

Sub CompareVals()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim YourRangeA As Range, YourRangeB As Range
Dim FoundCell As Range, ALastRow As Long
Dim i As Variant

Set ws1 = ActiveWorkbook.Worksheets(1)
Set ws2 = ActiveWorkbook.Worksheets(2)
ALastRow = ws1.Cells(Cells.Rows.Count, 1).End(xlDown).Row
Set YourRangeA = ws1.Range("A1:A" & ALastRow)
Set YourRangeB = ws2.Range("A:IV")

For Each i In YourRangeA
Set FoundCell = YourRangeB.Find(What:=i, LookIn:=xlValues)
With YourRangeB
.Find What:=i, LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True
If FoundCell Is Nothing Then
i.Interior.ColorIndex = 6
ElseIf Not FoundCell Is Nothing Then
Application.StatusBar = "Found Value " & FoundCell
End If
End With
Next i
End Sub



"Dave L" wrote:

I'm having an issue with the macro code below. Basically it's looking for a
value in one worksheet and then assigning that value to a variable called
"Item." Then it looks in another worksheet for the value. If it doesn't find
it it goes back to the first sheet and highlights it. The problem I'm running
into is that the same value can be in the first worksheet more than once.
It's not highlighting the first instance of the value but it is highlighting
every instance after it, which I don't want it to do. Any ideas?

With Sheets("Kits").Range("C1:C150000")
Set Rng = .Find(What:=Item, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)

If Not Rng Is Nothing Then
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
Else
With Selection.Interior
.Pattern = x1Solid
.PatternColorIndex = x1Automatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveCell.Offset(1, 0).Activate
Item = ActiveCell.Value
End If
End With

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Values in a cell Malik Excel Discussion (Misc queries) 3 April 3rd 08 09:13 PM
Finding a cell given x and y values farful[_2_] Excel Programming 1 July 12th 06 07:37 PM
Problem with file finding macro Alf[_8_] Excel Programming 6 June 11th 06 10:04 PM
finding cell values mellowe Excel Discussion (Misc queries) 2 October 20th 05 09:08 PM
Problem finding duplicate values nicoll Excel Programming 1 September 28th 03 10:46 PM


All times are GMT +1. The time now is 10:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"