Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default matching rows between worksheets/books - best practice

Hi,

Something of a newbiw to Excel VBA so please excuse me if this is an obvious
question.

I have two sheets. One contains original data, the other essentially the
same data just ordered and formatted in a different way. Each row is
identified with a unique ID.

I require a function that will loop through each row of the source sheet,
find the matching row (using the ID) on the destination sheet and then do
the required formatting. However I'm unsure how to do the match, I've looked
at the vlookup worksheetfunction but I'm unsure how it handles not being
able to find the ID (i.e. the source row isn't on the destination).

The approach I have taken is:

' Find the last row of source data and assign it to variable.

for icount = 1 to lastrow
'lookup value in destination here
'if found do various things. not applicable to this question
next icount

What is the best practice for looking up the ID's between the two sheets?
Vlookup, find, programming loops, etc.

Any and all advice is gratefully received.

Kind thanks

Chris.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default matching rows between worksheets/books - best practice

Dim rng1 as Range, rng2 as Range
Dim cell1 as Range, cell2 as Range
Dim res as Variant
' set source range
With worksheets("Sheet1")
set rng1 = .Range(.Cells(1,1),.Cells(rows.count,1).End(xlup))
End With
' set range on sheet2
With worksheets("Sheet2")
set rng2 = .Range(.Cells(1,1),.Cells(rows.count,1).End(xlup))
End With

for each cell1 in rng1
res = application.Match(cell1.value,rng2,0)
if not iserror(res) then
set cell2 = rng2(res)
' use cell2 to do your formatting
' for example
cell2.Interior.ColorIndex = 5
end if
Next

--
Regards,
Tom Ogilvy


"Chris Strug" wrote in message
...
Hi,

Something of a newbiw to Excel VBA so please excuse me if this is an

obvious
question.

I have two sheets. One contains original data, the other essentially the
same data just ordered and formatted in a different way. Each row is
identified with a unique ID.

I require a function that will loop through each row of the source sheet,
find the matching row (using the ID) on the destination sheet and then do
the required formatting. However I'm unsure how to do the match, I've

looked
at the vlookup worksheetfunction but I'm unsure how it handles not being
able to find the ID (i.e. the source row isn't on the destination).

The approach I have taken is:

' Find the last row of source data and assign it to variable.

for icount = 1 to lastrow
'lookup value in destination here
'if found do various things. not applicable to this question
next icount

What is the best practice for looking up the ID's between the two sheets?
Vlookup, find, programming loops, etc.

Any and all advice is gratefully received.

Kind thanks

Chris.




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
matching 2 worksheets melody_ph Excel Worksheet Functions 1 May 16th 09 02:35 AM
looking for changes in worksheets or books mlong Excel Worksheet Functions 0 May 14th 08 06:24 PM
matching two worksheets and return to different value dan Excel Worksheet Functions 2 October 23rd 06 11:18 PM
matching 2 worksheets together RayB Excel Discussion (Misc queries) 1 July 7th 06 08:07 PM
Trouble copying worksheets between books - is there size limit? kris2u Excel Worksheet Functions 2 October 13th 05 08:44 PM


All times are GMT +1. The time now is 04:17 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"