PC Review


Reply
Thread Tools Rate Thread

compare value of cell in one sheet to range of another sheet

 
 
Matthew Dyer
Guest
Posts: n/a
 
      9th Mar 2010
Still learning how to use VB coding to it's most effectiveness...

I have two sheets. sheet1 and sheet2. I want to pull the value of
cell(2, "d") from sheet1 and compare it to every value of column "d"
in sheet2, highlighting as it goes by. Once it has finished I want it
to go to cell(3, "d") in sheet1 and do the same thing, comparing
itself to all the values of column "d" in sheet2. I know how to pull
the variable I want, I know how to do the highlighting code, and I
know how to do the loops and whatnot. What I do not know how to do is
referance seperate sheets. help??
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      9th Mar 2010
Hello Matthew,

I found it was going to be a bit difficult to describe the use of assigned
ranges without actually writing example code required to do what you were
referring to.

Basically when a range is assigned to a range variable using the Set
statement, the range can be referred to from anywhere without further
reference to the worksheet to which it belongs because the worksheet info is
embedded in the range variable.

If you are using a different method of looping other than the For Each and
you want to use the range variable in your code then I will try to
incorporate it if you post the code you have.

Also I was not sure what you meant by Highlight so I just set the interior
color to yellow.

Sub Example()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rngDsht1 As Range
Dim rngDsht2 As Range
Dim c As Range
Dim cToFind As Range
Dim firstAddr As String

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")

With ws1
Set rngDsht1 = .Range(.Cells(2, "D"), _
.Cells(.Rows.Count, "D").End(xlUp))
End With

With ws2
Set rngDsht2 = .Range(.Cells(2, "D"), _
.Cells(.Rows.Count, "D").End(xlUp))
End With

For Each c In rngDsht1

With rngDsht2
Set cToFind = .Find(What:=c.Value, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not cToFind Is Nothing Then
firstAddr = cToFind.Address
Do
cToFind.Interior.ColorIndex = 6
Set cToFind = .FindNext(cToFind)
Loop While cToFind.Address <> firstAddr
End If
End With

Next c

End Sub


--
Regards,

OssieMac


 
Reply With Quote
 
Matthew Dyer
Guest
Posts: n/a
 
      11th Mar 2010
On Mar 8, 9:32*pm, OssieMac <Ossie...@discussions.microsoft.com>
wrote:
> HelloMatthew,
>
> I found it was going to be a bit difficult to describe the use of assigned
> ranges without actually writing example code required to do what you were
> referring to.
>
> Basically when a range is assigned to a range variable using the Set
> statement, the range can be referred to from anywhere without further
> reference to the worksheet to which it belongs because the worksheet infois
> embedded in the range variable.
>
> If you are using a different method of looping other than the For Each and
> you want to use the range variable in your code then I will try to
> incorporate it if you post the code you have.
>
> Also I was not sure what you meant by Highlight so I just set the interior
> color to yellow.
>
> Sub Example()
>
> Dim ws1 As Worksheet
> Dim ws2 As Worksheet
> Dim rngDsht1 As Range
> Dim rngDsht2 As Range
> Dim c As Range
> Dim cToFind As Range
> Dim firstAddr As String
>
> Set ws1 = Sheets("Sheet1")
> Set ws2 = Sheets("Sheet2")
>
> With ws1
> * Set rngDsht1 = .Range(.Cells(2, "D"), _
> * * .Cells(.Rows.Count, "D").End(xlUp))
> End With
>
> With ws2
> * Set rngDsht2 = .Range(.Cells(2, "D"), _
> * * .Cells(.Rows.Count, "D").End(xlUp))
> End With
>
> For Each c In rngDsht1
>
> * With rngDsht2
> * * Set cToFind = .Find(What:=c.Value, _
> * * * LookIn:=xlFormulas, _
> * * * LookAt:=xlWhole, _
> * * * SearchOrder:=xlByRows, _
> * * * SearchDirection:=xlNext, _
> * * * MatchCase:=False, _
> * * * SearchFormat:=False)
>
> * * If Not cToFind Is Nothing Then
> * * * firstAddr = cToFind.Address
> * * * Do
> * * * * cToFind.Interior.ColorIndex = 6
> * * * * Set cToFind = .FindNext(cToFind)
> * * * Loop While cToFind.Address <> firstAddr
> * * End If
> * End With
>
> Next c
>
> End Sub
>
> --
> Regards,
>
> OssieMac


Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")


how do referance a different workbook using the above code?
 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      12th Mar 2010
Hi Matthew,

Try the following. Both workbooks need to be open with the code I have
provided.

Sub Example()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rngDsht1 As Range
Dim rngDsht2 As Range
Dim c As Range
Dim cToFind As Range
Dim firstAddr As String

Set wb1 = Workbooks("MyBook1.xls")
Set wb2 = Workbooks("MyBook2.xls")

Set ws1 = wb1.Sheets("Sheet1")
Set ws2 = wb2.Sheets("Sheet2")

With ws1
Set rngDsht1 = .Range(.Cells(2, "D"), _
.Cells(.Rows.Count, "D").End(xlUp))
End With

With ws2
Set rngDsht2 = .Range(.Cells(2, "D"), _
.Cells(.Rows.Count, "D").End(xlUp))
End With

For Each c In rngDsht1

With rngDsht2
Set cToFind = .Find(What:=c.Value, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not cToFind Is Nothing Then
firstAddr = cToFind.Address
Do
cToFind.Interior.ColorIndex = 6
Set cToFind = .FindNext(cToFind)
Loop While cToFind.Address <> firstAddr
End If
End With

Next c

End Sub


--
Regards,

OssieMac


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Same cell range on different sheet unit2436@gmail.com Microsoft Excel Charting 2 19th Jun 2006 11:15 PM
After adding a new sheet, wanting to compare cell values Emmert Microsoft Excel Programming 2 16th Jun 2006 09:14 PM
Help me, compare 2 sheet and extract the match data into the new sheet. sweetnet Microsoft Excel Misc 1 22nd Feb 2006 07:49 PM
Compare Sheet Cell to Sheet Column =?Utf-8?B?QnJlbmRh?= Microsoft Excel Worksheet Functions 2 4th Jan 2006 07:32 PM
Automatically entering sheet names into a range of cells on one sheet u539771 Microsoft Excel Worksheet Functions 2 23rd Jun 2004 12:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:43 PM.