Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with either loop or status bar or both!!!

Status
Not open for further replies.

Shippwreck

Programmer
Oct 20, 2003
136
GB
Hi,

It would seem to be a simple problem, and i have a working solution, however as with all things it don't work properly (or at least how i want it to)

The problem is that i have 2 spreadsheets that i need to compare. Both have the same column headings and both contain a table that is 350ish rows by 13 columns. What i need to do is compare the two as they both contain staff data and identify joiners and leavers (tables from different quarters) What i decided was to look at each row in the first table one by one and loop through the second table searching. If it found a match it puts the field into "no change" tab, if they are not there it puts the field into "Joiners" tab, and then anyone left must be a leaver.

The second part to this is that i relised i could use this to clean the data records at the same time, so only if all 13 fields are exactly the same will it put it in "no change", that way any left are either leavers or need correct data. (as it happens i only care about the ones that stay the same or are joiners so the fact that all leavers and the ones with incorrect data will be in the same sheet doesn't matter as this sheet will only be deleted anyway)

Due to the size of the sheets and the number of potential comparisons it takes a good 10 min to run so i tried to add a status bar to indicate percent complete. This is where the problem comes, because although the bar works perfectly it only half displays, it seems to show the border for the form, but nothing shows in the form, if you then open up another app and move it over and away from the bar, the rest appears, but that is obviously not workable

I run the macro by calling the form and then the compare loop is called from the form.

The loop and form code is below, (form code in bold), there is obviously a form to this as well but i can't show that. The 2 questions i have are:

Is there anyway to improve the loop first? (I have never written anything like this before and this is my first stab so i'm fully open to the possibilty that it is not very efficient, and this could possibly be the reason for the form not displaying properly)

If that isn't the reason and the code is relatively good then why wont the form display properly and how can i get it to display properly?

Code:
For ccrownum = 2 To cclastrow
    For dhrownum = 2 To dhlastrow
    If Match = 13 Then
        Exit For
    End If
    Match = 0
        For cccolumnnum = 1 To cclastcolumnnum
            Sheets("Current Staff").Select
            Range(Chr$(cccolumnnum + 64) & ccrownum).Select
            ccRecord = ActiveCell.Value
            Sheets("DH Return").Select
            Range(Chr$(cccolumnnum + 64) & dhrownum).Select
            If ActiveCell.Value = ccRecord Then
                Match = Match + 1
                If Match = cclastcolumnnum Then
                    Rows(dhrownum & ":" & dhrownum).Select
                    Selection.Cut
                    Sheets("No Change").Select
                    Range("A1").Select
                    Selection.End(xlDown).Select
                    If ActiveCell.Row = 65536 Then
                        Range("A2").Select
                    Else
                        ActiveCell.Offset(1, 0).Select
                    End If
                    ActiveSheet.Paste
                    Match = 0
                    Exit For
                End If
            Else
                Exit For
            End If
        Next cccolumnnum
    Next dhrownum
Sheets("Current Staff").Select
Rows(ccrownum & ":" & ccrownum).Select
Selection.Cut
Sheets("Joiners").Select
Range("A1").Select
Selection.End(xlDown).Select
    If ActiveCell.Row = 65536 Then
        Range("A2").Select
    Else
        ActiveCell.Offset(1, 0).Select
    End If
ActiveSheet.Paste
[b]PctDone = ccrownum / cclastrow
With UserForm1
    .Label1.Caption = Format("Percent complete")
   .FrameProgress.Caption = Format(PctDone, "0%")
    .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
End With[/b]
Next ccrownum
[b]Unload UserForm1[/b]

Thanks for any help

Shippwreck

 
Hi Shippwreck,

I'll have a look at your code - it shouldn't be taking ten minutes for just 350 rows or so - but immediately, you need to add DoEvents after updating your label so that the display can be refreshed.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Well i'll be, that DoEvents works lovely.

Actually i have the same piece of code working on another spreadsheet that has considerably less calculations and the form displays perfectly without the DoEvents. I should have mentioned that i didn't actually write the form code, i copied it from a web site (however i can't remember which one otherwise i would mention them for credit purposes) so i was pretty certain the form code was sound, however obviously it wasn't quite there

Additionally i have added a counter to the code and it does 122589 comparisons on the sheets that i currently use. The data on the sheets is not very good (due to improper data submissions before, hence the need for this code) and it currently assigns all staff to joiners as almost all have incorrect details. (However i did check that matches are handled correclty by adding a few matches into the data yself). Therefore it is doing the absolute maximum number of comparisons it is likely to have to do. Therefore i would expect the run time to half with acurate data, however 5 mins is still a lot.

Thank you very much for taking the time to look at my code

Shippwreck

 
Hi Shippwreck,

Sorry, didn't get round to your code yet, but I would think the easiest way to match your two lists would be to use Advanced Filtering using one list of names as the criteria for the other sheet. Remove the ones which match from each sheet and you have your leavers and joiners. It shouldn't take more than 5 minutes manually (and you can record the code for next time)! You still have to do the rest of the validation/cleaning but it should be a snip once you've done the matching.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi,

Yeah i had thought about using advanced filtering, however i decided not to as i wanted to try something new in VBA, expand my skills etc. I know that from an actual using the sheet point of view it is much better to have it very quickly, however as you said the cleaning task is still left over and inevitably some fields would be missed when you're looking at 300*13 fields.

One problem that has now cropped up is that when i compare a certain field (which is formatted exactly the same way in both sheets) it doesn't match when they do. The reason is that it seems to put speechmarks round the second value, but not the first and therefore they fail to match. the code segment in question is below:

Code:
ccRecord = ActiveCell.Value
Sheets("DH Return").Select
Range(Chr$(cccolumnnum + 64) & dhrownum).Select
If ActiveCell.Value = ccRecord Then

If you or anyone else knows why please let me know.

Anyway thanks for the help with the status bar and if you or anyone else does get a chance to look at the loop code that would be great

thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top