Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Matching values in different columns/workbooks


Hi, The code below loops through each cell in one column of data and
finds the same value in another column (on another spreadsheet). This
works fine for a 100 cells, but the final version will check 600 rows
in seven worksheets, and this takes an age. How can I do this more
efficiently? Would it be better to use an array (not sure how to do
this), and I assume that when the correct value is found, the
remaining cells are still searched? Could I use a function which finds
the match and so skips checking the rest of the column?


For Each r In Workbooks("02 - Scheduler
Plan.xls").Worksheets(SchedulerDayName).Range("B2: B100").Cells
If IsNumeric(r.Value) Then
For Each c In Workbooks("00 -
MDS.xls").Worksheets(MDSDayName).Range("C4:C1000") .Cells
If c.Value = r.Value Then
c.offset(0, 2).Resize(1, 145).Copy
r.offset(0, 4).PasteSpecial xlPasteValues
End If
Next c
End If
Next r

hope you can help.

regards,
Matt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Matching values in different columns/workbooks

make sure you turn off calculation and screen updating before running your code:

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' your code

Application.ScreenUpdating =True
Application.Calculation = xlCalculationAutomatic


--


Gary


"MJKelly" wrote in message
...

Hi, The code below loops through each cell in one column of data and
finds the same value in another column (on another spreadsheet). This
works fine for a 100 cells, but the final version will check 600 rows
in seven worksheets, and this takes an age. How can I do this more
efficiently? Would it be better to use an array (not sure how to do
this), and I assume that when the correct value is found, the
remaining cells are still searched? Could I use a function which finds
the match and so skips checking the rest of the column?


For Each r In Workbooks("02 - Scheduler
Plan.xls").Worksheets(SchedulerDayName).Range("B2: B100").Cells
If IsNumeric(r.Value) Then
For Each c In Workbooks("00 -
MDS.xls").Worksheets(MDSDayName).Range("C4:C1000") .Cells
If c.Value = r.Value Then
c.offset(0, 2).Resize(1, 145).Copy
r.offset(0, 4).PasteSpecial xlPasteValues
End If
Next c
End If
Next r

hope you can help.

regards,
Matt



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Matching values in different columns/workbooks

Thanks Gary.
I know this would speed up the routine, but is the approach I've used
sound? I thought an array would make this faster and I'm sure there
must be a way of moving to the next step instead of checking the
remainder of a column once the match has been found?

Matt
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Matching values in different columns/workbooks

Hi Matt

I don't think you can improve the routine to speed it up except from adding
"Exit For" after this linge:

r.Offset(0, 4).PasteSpecial xlPasteValues

Hopes it helps.

Regards,
Per

"MJKelly" skrev i meddelelsen
...
Thanks Gary.
I know this would speed up the routine, but is the approach I've used
sound? I thought an array would make this faster and I'm sure there
must be a way of moving to the next step instead of checking the
remainder of a column once the match has been found?

Matt


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Matching values in different columns/workbooks


Thanks for this Gents, the code is running faster now (66% faster!).

Ardus,
When I tried using your suggestion I got a run-time error (438)?

Per,
Is the "exit for" skipping the remainder of the column once a match
has been found? If so this is what I was trying to do. Thanks very
much.

Kind regards,
Matt


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Matching values in different columns/workbooks

Hi Matt

Thanks for your reply.

The "exit for" is skipping the inner for/next loop once a match has been
found in the column, and continues with next value in column B.

Best regards
Per


"MJKelly" skrev i meddelelsen
...

Thanks for this Gents, the code is running faster now (66% faster!).

Ardus,
When I tried using your suggestion I got a run-time error (438)?

Per,
Is the "exit for" skipping the remainder of the column once a match
has been found? If so this is what I was trying to do. Thanks very
much.

Kind regards,
Matt


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default Matching values in different columns/workbooks

Use UsedRAnge instead of Cells.
Cells returnrs ALL cells in your worksheet
UsedRange returns only used cells


For Each r In Workbooks("02 - Scheduler
Plan.xls").Worksheets(SchedulerDayName).Range("B2: B100").UsedRange
If IsNumeric(r.Value) Then
For Each c In Workbooks("00 -
MDS.xls").Worksheets(MDSDayName).Range("C4:C1000") .UsedRange
If c.Value = r.Value Then
c.offset(0, 2).Resize(1, 145).Copy
r.offset(0, 4).PasteSpecial xlPasteValues
End If
Next c
End If
Next r

HTH
--
AP


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Matching values in different columns/workbooks

This

r.Offset(0, 4).Resize(1, 145) = c.Offset(0, 2).Resize(1, 145).Value

instead of

* * * * * * c.offset(0, 2).Resize(1, 145).Copy
* * * * * * r.offset(0, 4).PasteSpecial xlPasteValues


should work much faster.

My solution is

For Each r In Workbooks("02 -
SchedulerPlan.xls").Worksheets(SchedulerDayName).R ange("B2:B100")
If IsNumeric(r.Value) Then
countNames = WorksheetFunction.CountIf(Workbooks("00 -
MDS.xls").Worksheets(MDSDayName).Range("C4:C1000") , r.Value)
If countNames 0 Then
Set c =
Workbooks("MDS.xls").Worksheets(1).Range("C4:C1000 ").Find(r.Value,
LookAt:=xlWhole)
r.Offset(0, 4).Resize(1, 145) = c.Offset(0, 2).Resize(1,
145).Value
End If
End If
Next

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Matching values in different columns/workbooks

Per,
Thanks, I thought that's what it must be doing.

nikoiao,
Thanks very much. Yes it does run much faster again. I guess because
I am not copying pasting the speed is increased?

Thanks everyone for your help. The code now runs in less than 4
seconds where as before it never really finished, it was just hanging,
and even applying these suggestions one at a time, the code has run
around 60% faster.

I have been told that copying/pasting takes up memory and excel is
pretty rubbish at clearing this memory, so over time as the
application is open and used it will start to run slower. Is this
your experience/understanding? This is why I was interested in using
an array as I am told the memory can be cleared?

Thanks,
Matt
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 Values in Two Columns John Excel Discussion (Misc queries) 2 October 27th 09 01:23 PM
Matching values in 2 columns richzip Excel Discussion (Misc queries) 1 January 24th 08 11:40 AM
Copy certain columns matching column A in two workbooks Chuckak Excel Programming 3 February 14th 07 05:28 AM
Matching values in two columns Rob Excel Discussion (Misc queries) 4 December 6th 06 04:44 AM
Matching values in 2 columns Aonghus Excel Programming 2 November 9th 05 03:31 PM


All times are GMT +1. The time now is 12:32 PM.

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"