MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 9th, 2002, 08:30 PM   #1
FredMFoley
Board Regular
 
Join Date: Mar 2002
Location: Tasmania
Posts: 58
Default

I would appreciate any help to speed this sub up please as my knowledge of VBA is still very limited.

Column A on Sheet1 contains about 1500 unique values (no blanks).
Column A on Sheet2 contains about 1000 of the Sheet1 values (no blanks).
Neither column is sorted and both sheets have data in other columns.

My sub finds the matching values in the two A Columns, then transfers data IN OTHER COLUMNS from one sheet to the other, and puts it in the matching row.

While I'm pleased that it works, it is frustratingly slow (about 80sec), and I'm sure there would be much faster ways to do it.



Sub CompareAndTransfer()
Dim i%, j%, OldCodes%, NewCodes%, NewColumn%, NewSheet$, TextFile$, Codes$

For i = 1 To OldCodes '2 allows for heading
For j = 1 To NewCodes
If Sheets("Codes").Cells(i, 1) = Sheets(TextFile).Cells(j, 1) _
Then Sheets("Codes").Cells(i, 4) = Sheets(TextFile).Cells(j, 6) _
: j = NewCodes 'jump out when found (to save time)
Next j
Next i
End Sub

Thank You
Fred
FredMFoley is offline   Reply With Quote
Old May 9th, 2002, 08:52 PM   #2
Qroozn
Board Regular
 
Join Date: Mar 2002
Location: Sydney/Brisbane , Australia
Posts: 539
Default

When you perform this macro does the screen flash in front of you as it makes the changes?
__________________
Colin
Qroozn is offline   Reply With Quote
Old May 9th, 2002, 08:59 PM   #3
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi Fred,

Its already reasonably efficient as you have it. These changes might help a bit by getting as many operations out of the inner loop as possible:

Sub CompareAndTransfer()

Dim i%, j%, OldCodes%, NewCodes%, NewColumn%, NewSheet$, TextFile$, Codes$
Dim Cell1 As Range
Dim Cell4 As Range
Dim WST As Worksheet

' don't forget to set TextFile$ and NewSheet$, etc.

Set WST = Worksheets(TextFile)

With Sheets("Codes")
For i = 1 To OldCodes '2 allows for heading
Set Cell1 = .Cells(i, 1)
Set Cell4 = .Cells(i, 4)
For j = 1 To NewCodes
If Cell1.Value = WST.Cells(j, 1) Then
Cell2.Value = WST.Cells(j, 6)
Exit For 'jump out when found (to save time)
End If
Next j
Next i

End With
End Sub
__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old May 9th, 2002, 09:04 PM   #4
FredMFoley
Board Regular
 
Join Date: Mar 2002
Location: Tasmania
Posts: 58
Default

Thanks for the reply.

I have tried it with and without

Application.ScreenUpdating = False
Sub
End Sub
Application.ScreenUpdating = True

and it makes very little difference
FredMFoley is offline   Reply With Quote
Old May 9th, 2002, 09:21 PM   #5
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hi, Another way...


Sub TryThis()
Dim rngList1 As Range, rngList2 As Range
Dim strBuf() As String, rngTmp As Range, strNewBuf() As String
Dim ret, LngN As Long, lngCnt As Long, strTmp As String
'Assume
Dim TextFile As String
TextFile = "TextFile"

With Sheets("Codes")
Set rngList1 = .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
End With
With Sheets(TextFile)
Set rngList2 = .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
End With

For Each rngTmp In rngList1
ret = Application.Match(rngTmp.Value, rngList2, 0)
If Not IsError(ret) Then
strTmp = Sheets(TextFile).Cells(ret, 6).Value
Else
strTmp = ""
End If
LngN = LngN + 1
ReDim Preserve strBuf(1 To LngN)
strBuf(LngN) = strTmp
Next

ReDim strNewBuf(1 To LngN, 1 To 1)
For lngCnt = 1 To LngN
strNewBuf(lngCnt, 1) = strBuf(lngCnt)
Next
With Sheets("Codes")
.Columns(4).ClearContents
.Cells(1, 4).Resize(LngN).Value = strNewBuf()
End With
End Sub

__________________
Regards,

Masaru Kaji aka Colo - Ex Microsoft MVP Since 2004 -2009
Colo is offline   Reply With Quote
Old May 9th, 2002, 09:31 PM   #6
Nimrod
MrExcel MVP
 
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
Default

Instead of going through the Target list every time ie 1500x1000 go directly to the Target Row with the find command. Here's some code to give you a general idea. You will need to modify the sheet name etc. Best look in your VB help for a total picture.

Code:
Public Sub FindAndReplace()

For i = 1 To OldCodes '2 allows for heading
ValueToFind = Sheets("Codes").Cells(i, 1)

With Worksheets(1).Range("a1:a1000")
    Set c = .Finds(ValueToFind,LookIn:=xlValues)
   TargRow = c.Row
End With
 Sheets("Codes").Cells(i, 4) = Sheets(TextFile).Cells(TargRow, 6)
Next 
End Sub
This method will cut your loops down from 1000x1500 to just 1000 !!!

[ This Message was edited by: Nimrod on 2002-05-09 20:33 ]

[ This Message was edited by: nimrod on 2002-05-09 20:58 ]
Nimrod is offline   Reply With Quote
Old May 9th, 2002, 09:46 PM   #7
FredMFoley
Board Regular
 
Join Date: Mar 2002
Location: Tasmania
Posts: 58
Default

Thanks for all the help.
It'll take me quite a while to absorb all the feedback.
Fred
FredMFoley is offline   Reply With Quote
Old May 9th, 2002, 11:55 PM   #8
FredMFoley
Board Regular
 
Join Date: Mar 2002
Location: Tasmania
Posts: 58
Default

Thanks Nimrod
When I use your code this line

Set c = .Finds(ValueToFind, LookIn:=xlValues)

Causes this error:
Object doesn't support this property or method

Can you tell me what is wrong here?
---------
I found the mistake - eventually! - Finds should have been Find.
After making minor changes this method has cut the time down to about 3 or 4 seconds!!
Thanks again Nimrod


Damon's solution cut the time down from 80 sec to about 30sec, and I haven't tackled Colo's yet - It looks a bit daunting to me.

Thanks for all the help.


[ This Message was edited by: FredMFoley on 2002-05-11 04:34 ]
FredMFoley is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On

Forum Jump


All times are GMT -4. The time now is 03:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes