+ Reply to Thread
Results 1 to 18 of 18

VBA Macro not completing it's task.

  1. #1
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    VBA Macro not completing it's task.

    Hi, I finally wrote a couple of macros in VBA that I wanted to do and they work but for some reason they do not complete the task properly and I have to run the macro over and over again to finish completely. For instance I wrote a program in VBA to remove rows of data that contain duplicate cells in the same row. This program goes thru a spead sheet row by row and compares the values in the first cell with the next cell, so on and so forth until the whole row is checked for either a duplicate or a value that is higher than the next cell. If either condition is met then the program deletes the row and moves on to the next row checking in the same manner and shifts up to remove the empty rows. But, the program does not complete its task on the first try. For a spreadsheet of about 700 rows it takes about 6 or 7 runs of the macro to remove all duplicates ect... Why is this?

    Here's a copy of the code:

    Option Explicit

    Please Login or Register  to view this content.
    Can anyone tell me why I'm having an issue with incomplete macro tasking?

    Thanks,

    Sebastien.
    Last edited by Cutter; 09-03-2012 at 09:42 AM. Reason: Added code tags

  2. #2
    Registered User
    Join Date
    08-31-2012
    Location
    HCM City, VN
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA Macro not completing it's task.

    Because you had used COUNT function, it didn't count blank cells. For example, you have 12 number cells and 3 blank cells (A2, A4, A6). When you use COUNT function, the result is 12. After that, your looping is playing from A1 to A12.

    Using LastRow = Range("A65000").End(xlUp).Row, LastRow = 15 and your looping is playing from A1 to A15. And this macro will complete your task.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: VBA Macro not completing it's task.

    @ sgrondines

    Welcome to the forum.

    Please notice that code tags have been added to your post(s). The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at top of the page and read Rule #3.
    Thanks.

  4. #4
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    I'm not sure I understand your explanation about the blank cells. There are no blank cells within the rows of numbers I'm checking, cells 1 to 6 in each row. Nevertheless I tried changing the code by adding the new "LastRow" statement and still had to run the macro 7 times to remove all of the rows with duplicate numbers in them. The code runs and doesn't error out or anything. I'm not sure why, that when the code does run it works but has to run 7 times to completely do it's task.

    PS. Forum Moderator: Sorry about that. I'm new to using Forum's and I'm having a hard time figuring out how to use them sometimes. Like even how to post. I'll try go through the rules a little better next time.

    Thanks.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    You needto work from the bottom up when deleting rows

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    You needto work from the bottom up when deleting rows

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    Likewise, I'm having the same problem with another code I wrote having the same set up but it is suppose to delete every x number of rows from an input and shift the empty cells up. It works fine like the other code but does not finish it's task again. So if I have 400 lines of data, and I want to delete every 4th row. I should have 100 lines of data left, but I have to run this macro several times again to get the desired result.

    The code is as follows:


    Please Login or Register  to view this content.
    There must be an issue that excel has with this kind of configuration which can't run fast without missing stuff. Looping throuh in a stepwise fashion this code works perfectly but doesn't not at full speed. With that said I haven't stepped through the code until the end of the over 700 lines of data.

    Thanks.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    And Im not why you looping because in the first time it finds where
    Please Login or Register  to view this content.
    its deleting that row.

  9. #9
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    Sorry, I didn't see mike7952's post. I tried your code and it works but could you please explain to me why my method doesn't work? I would really appreciate that.

    Thanks.

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    So if I have 400 lines of data, and I want to delete every 4th row. I should have 100 lines of data left
    I think your math is a little off if you divide 400 by 4 is 100. So its deleting 100 rows

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    This is because if you delete a row working from the 'top down', rows will be skipped when each row is deleted.

  12. #12
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    Yah, I think your right about the math. Haven't had my second cup of coffee yet. lol. So, then if I'm going to delete from the bottom up the deleting of the xth row will start from the bottom not from the top. ie. If I want the 4th row removed from my data I should look at the bottom rows of data and make my decision from there?

    Thanks.

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Macro not completing it's task.

    Please Login or Register  to view this content.
    Is there cell zero exist in excel?

  14. #14
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    To AB33. No, I'm new to programming and I read that it is good to initialize variables to zero before beginning.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Macro not completing it's task.

    Numeric variables in VBA initialize to zero, strings to zero length, objects to Nothing.
    Entia non sunt multiplicanda sine necessitate

  16. #16
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    I have a problem that has come up now. I have put a button on the spreadsheet which I am running the delete rows macro on which runs the macro when clicked. But when the code deletes the rows according to the program it sometimes deletes part or most of the macro button on the sheet so it's no longer visible or only partly visible. How do I prevent this?

    Thanks.

  17. #17
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: VBA Macro not completing it's task.

    Oh, I think I figured the deleted button thing out. In properties for the format button, I selected "Don't move or size with cells". Is that correct?

    Thanks everyone for your help. You all rock! This thread is basically resolved. I still am having a problem with the DeleteXRows code which I changed to deleting from the bottom up.It seems that the code deletes the last row first then every x rows from there. How do I get the code to start at the xth row from the bottom in the loop as the first delete? If I can get this answered I'm pretty satified with ending this thread though I wish I could run the code deleting from the first line to the end instead of the other way around.

    Thanks Everyone.

  18. #18
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA Macro not completing it's task.

    Try his then

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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