Solved

Replacing a "Do... Loop Until" routine with something a littlef aster

Posted on 2011-03-02
7
265 Views
Last Modified: 2012-05-11
I have a code that goes back and forth between two workbooks in order to identify matches between them and hide the rows for which there is no match. It works very well, with no screen flicker and no visible moving back and forth between books, and it doesn't take all that long, about a minute and a half; but it's always nice to dazzle whenever possible :-)

Is there a way to do this faster? Given the fact that it has to go back and forth between the two books?

Thanks,
John
Sub FindMatches()
Application.ScreenUpdating = False
Do
ActiveWindow.ActivateNext
Dim str As String
str = ActiveCell.Value

ActiveCell.Copy
ActiveCell.Offset(1, 0).Activate
ActiveWindow.ActivateNext
    Cells.Find(What:=str, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Interior.ColorIndex = 45
ActiveCell.Offset(0, -1) = 1
Dim top As Range, btm As Range
Set top = [C6]
Set btm = ActiveCell
Range(top, btm).Select
For Each cel In Range(top, btm)
    If cel.EntireRow.Interior.ColorIndex <> 45 Then
    cel.EntireRow.Hidden = True
    Else
    End If
Next
btm.Offset(1, 0).Activate
Loop Until IsEmpty(ActiveCell)
[Counts].Calculate
[C2].Activate
Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:gabrielPennyback
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35023590
>>> Is there a way to do this faster?

1) Yes. Instead of using Active Window, store the names of the workbook in the Workbook object so you don't need to activate it.

2) Avoid .Select and directly perform the action on that cell for example you don't need line 19 in the above code.

If you supply the workbook names and explain exactly what you are trying to do then maybe I can cough up some code for you :)

Sid
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 35023684
Hi Sid, thanks. The calling workbook is named "SDAE_1.xls" and the the other workbook is named "KRG_1.xls"
Thanks for pointing out line 19. I just had it in there for testing. I always do that, and often forget to remove it.

Yes please let me know how to use the stored names of the workbooks in the code, and of course how to do the storing in the first place!

And I'll really be interested to see if there's a way to go through the cells in both books without activating.

Thanks,
John
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35023756
John,

It would be good to see your books/code so we could tailor a response to this

as an example you can kill this entire For Loop section (the snippet below) - the usual culprit in slow code - by turning the logic on its head. instead

a) hiding all the rows in this range
b) finding all cells with a colorindex of 45 using Find (there is a Format option)
c) unhide these rows

cheers

dave
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Expert Comment

by:Dave Brett
ID: 35023759
<hmm, fell off again>
For Each cel In Range(top, btm)
    If cel.EntireRow.Interior.ColorIndex <> 45 Then
    cel.EntireRow.Hidden = True
    Else
    End If
Next

Open in new window

0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35023787
Here is a basic Idea on how to go about it. I have commented some code as I am unsure as to what you want to do.

Sub FindMatches()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim str As String
    Dim aCell As Range
    Dim top As Range, btm As Range
    
    Application.ScreenUpdating = False
    
    '~~> SDAE_1.xls
    Set wb1 = ActiveWorkbook
    
    lastrow = wb1.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    Set wb2 = Workbooks.Open("C:\KRG_1.xls")
    
    '~~> Looks for text from Col A of wb1 in wb2
    For I = 1 To lastrow
        str =wb1.Sheets("Sheet1").Range("A" & i).Value
    
        Set aCell = wb2.Sheets(1).Cells.Find(What:=str, After:=ActiveCell, _
        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
        
        If Not aCell Is Nothing Then
            aCell.EntireRow.Interior.ColorIndex = 45
            aCell.Offset(0, -1) = 1
            
            'Set top = [C6]
            'Set btm = ActiveCell
            'For Each cel In Range(top, btm)
                'If cel.EntireRow.Interior.ColorIndex <> 45 Then
                'cel.EntireRow.Hidden = True
                'Else
                'End If
            'Next
            'btm.Offset(1, 0).Activate
     Next I
    '[Counts].Calculate
    '[C2].Activate
    Application.ScreenUpdating = True
End Sub

Open in new window


Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35023810
Please note that the code that I gave you is just a general representation of your code. You might have edit paths, sheetnames, cell references etc. Also please note that you don't have to keep the other workbook open. The code will open it for you. Once your task is over, you may close the 2nd workbook by introducing the code

wb2.close (True)

True is to close the workbook with changes saved. If you want to ignores changes then replace true by false.

Sid
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 35032779
Thanks Sid. Your code would probably work if i could figure out how to adapt it properly but couldn't get it to work. However, as with the other question (http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26861872.html#a35029686), I took your reminder to avoid selecting so much and came up with something that does it in about 4 seconds.

So thanks again, again!

- John
Sub FindMatches()
Application.ScreenUpdating = False
[C6].Activate
Do
ActiveWindow.ActivateNext
Dim str As String
str = ActiveCell.Value
If str = "" Then
ActiveWindow.ActivateNext
[Counts].Calculate
Exit Sub
Else
End If

ActiveCell.Copy
ActiveCell.Offset(1, 0).Activate
ActiveWindow.ActivateNext
    Cells.Find(What:=str, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart).Activate
ActiveCell.EntireRow.Interior.ColorIndex = 45
ActiveCell.Offset(0, -1) = 1
Dim top As Range, btm As Range

Set x = ActiveCell.Offset(0, -1)
Set btm = x.Offset(-1, 0)
Set top = x.End(xlUp).Offset(1, 0)

If btm = "" Then
Range(top, btm).Select
Range(top, btm).EntireRow.Hidden = True
Else
End If

btm.Offset(1, 1).Activate
Loop Until IsEmpty(ActiveCell)
[Counts].Calculate
[A2].Activate
Application.ScreenUpdating = True
End Sub

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question