+ Reply to Thread
Results 1 to 14 of 14

Unrecognisable logical error

  1. #1
    Registered User
    Join Date
    04-19-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    1

    Question Unrecognisable logical error

    Hi! In the following code, I want to compare 'time'(in column 2 and column 4) of all 8002 rows if the cells (i,6) and (j,6) are equal. In the Excel sheet for which the macro has been written,
    Column 2 contains Start-time(of the beginning of the event).
    Column 3 contains the value of the column 2 values in type 'Number'.
    Column 4 contains End-time and Column 5 contains the values in type 'Number'.
    If the event is being recorded more than once for the same duration of time (for the same column 6 value), the jth row is to be deleted or before deleting, (j,4) has to be transposed to (i,4), in case (j,4) is larger than (i,4).
    Please Login or Register  to view this content.
    Every If statement contains atleast 2 lines in the body, so i used If-Then-Else in the same lines and no End If statements.
    The problem tht i am encountering is, that ,i am performing only 2 operations, 'transpose' or 'delete' or both. But nowhere am i changing the values(incrementing,decrementing). The value of (j,4)is itself being altered to some very random time value. Pleas help!

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Unrecognisable logical error

    It will be good if you attach the file that you are using along with the code so we can troubleshoot easily. Even a sample file in the same format as the original will do.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Unrecognisable logical error

    Blimey! A true example of spaghetti code! I can't see many people taking on that code, you need to restructure it and remove all of the Gotos to get some meaningful help.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Unrecognisable logical error

    I'm about half-way through re-writing it, but it's really, really hideous.

    I haven't seen code like that since I taught myself to code in BBC BASIC circa 1984.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Unrecognisable logical error

    Yeah, i agree too. With all the gotos, its messy & confusing.

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Unrecognisable logical error

    Quote Originally Posted by Andrew-R View Post
    I'm about half-way through re-writing it, but it's really, really hideous.

    I haven't seen code like that since I taught myself to code in BBC BASIC circa 1984.
    This is said with love and respect Andrew, but you are mad for even trying to unravel it

    A bit off-topic, but I was once a Cobol programmer in a previous life. We bought an electoral roll package from another council, but the package was truly horrible. The programmers were all fresh off of a course, and they had used a 'technique' called alterable Gotos. This was code where they directed the flow using Gotos, bad enough, but this particular compiler allowed the Goto lines to be directed somewhere else from within the code, and this package used it extensively. I cannot recall the syntax exactly, but this should give you the idea

    Please Login or Register  to view this content.
    Of course the code was much more complex than that, with myriad Gotos. All those Gotos, horrible, but chuck in the fact that some of the code suddenly doesn't go where it says it goes to and you can imagine the chaos. Luckily I had a great programmer working for me, very thorough, and he methodically worked through it all rewriting the code without that rubbish.
    Last edited by Bob Phillips; 04-19-2012 at 04:04 AM.

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Unrecognisable logical error

    OK, I think I've done this - although without data to test it against it's pretty much a guess.

    First off, some coding tips:

    1. It's generally agreed that it's best to avoid GoTo statements - they make the flow of the code much harder to understand. Use structured code, such as block If statements to eliminate them.

    2. Don't use single letter variable names and use Dim to declare variables before using them

    3. Nested loops are slow - in your code the inner loop will be executed more than 32,000,000 times. Excel has tools, such as the Find method, to avoid having to repeatedly compare cell against cell looking for a match.

    4. Did I mention the GoTo? Good, I really mean that one.


    Anyway, try this (make sure you save a copy of your data first, as it's untested):

    Please Login or Register  to view this content.

  8. #8
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Unrecognisable logical error

    Quote Originally Posted by Andrew-R View Post
    1. It's generally agreed that it's best to avoid GoTo statements - they make the flow of the code much harder to understand. Use structured code, such as block If statements to eliminate them.
    Okay, let's start a war .

    I disagree, and I think it is an area of debate rather than general agreement. Whilst I personally avoid them wherever possible, I cannot get exercised about Gotos in code. If the code is well structured, and the Gotos are used sparingly and in a logical flow, I cannot see any argument against them. What I think is unarguable is that usage such as shown here is horrible, terrible programming practice, and really should be avoided.

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Unrecognisable logical error

    @Bob - the nadir of my BBC days was when I tried to code a mini-operating system which worked on 4 letter commands. The opening lines looked like this:

    Please Login or Register  to view this content.
    Basically when the user typed in their 4 letter command the program would poke those 4 letters in place of the @ symbols on line 20 of the code and then GOSUB to that line call the procedure with that name and then use the RETURN to go back to the branch point.

    Amazingly it worked, but I wouldn't have liked to try to debug it.

  10. #10
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Unrecognisable logical error

    Quote Originally Posted by Andrew-R View Post
    OK, I think I've done this - although without data to test it against it's pretty much a guess.
    BTW, whilst I still think you are mad, and whilst I think your code is pretty much as spaghetti as the original, I am in awe that you took it on, and even deeper in awe that you finished it

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Unrecognisable logical error

    I agree - it's nasty code (note the Sub name), because without any proper data to work with the best I could do was re-write pretty much what was there, rather than doing it properly. Going from the OP's description there is obvious redundancy (e.g. column 3 and column 5 contain the same data as columns 2 and 4, just formatted differently) and tortuous routes taken for no good reason (e.g. comparing the hours and minutes of two cells separately to work out if one is greater than the other - was there a good reason for doing this?)

    On the subject of GoTo - if your philosohpy is to avoid them whenever possible then I maintain you'll never use them. I made the leap from BBC BASIC to a structured language (Pascal) in 1987 and in the 25 years since then I've never had to use a GoTo, except in error trapping. I've done quite a lot of coding in that time and while I admit it may be the case that I've just never coded something which needs GoTos I'd be tempted to say they're simply not required.

  12. #12
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Unrecognisable logical error

    Give that man a medal.

  13. #13
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Unrecognisable logical error

    Quote Originally Posted by Andrew-R View Post
    I agree - it's nasty code (note the Sub name), because without any proper data to work with the best I could do was re-write pretty much what was there, rather than doing it properly. Going from the OP's description there is obvious redundancy (e.g. column 3 and column 5 contain the same data as columns 2 and 4, just formatted differently) and tortuous routes taken for no good reason (e.g. comparing the hours and minutes of two cells separately to work out if one is greater than the other - was there a good reason for doing this?)
    The point I was making is that it is not Gotos that makes spaghetti code, it is spaghetti coding. It is just as easy to write spaghetti code without a single Goto - you admirably made that point for me

    Quote Originally Posted by Andrew-R View Post
    On the subject of GoTo - if your philosohpy is to avoid them whenever possible then I maintain you'll never use them. I made the leap from BBC BASIC to a structured language (Pascal) in 1987 and in the 25 years since then I've never had to use a GoTo, except in error trapping. I've done quite a lot of coding in that time and while I admit it may be the case that I've just never coded something which needs GoTos I'd be tempted to say they're simply not required.
    I agree, they are not required (error trapping excepted), but I don't agree that it then follows on that it is wrong to use them. As an analogy, classes are not required (except for some event handling), but I would strongly advocate using them. Similarly, it isn't required to modularise your code, but you should.

    But I must stop arguing for Gotos, it is far easier too say don't use them in the hope that we never see such an abomination again.
    Last edited by Bob Phillips; 04-19-2012 at 07:04 AM.

  14. #14
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Unrecognisable logical error

    Quote Originally Posted by PingPing View Post
    Give that man a medal.
    Pah, anything at all from the OP would have been nice.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1