+ Reply to Thread
Results 1 to 10 of 10

Error When Implementing Code: "1004 - Application-defined or object-defined error"

  1. #1
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Error When Implementing Code: "1004 - Application-defined or object-defined error"

    I have been writing a small macro that takes data from a database and trims out the useless data that I get. when I run this code it works for the first third of the trimming. Then when it gets near the end of the list I get the following error:

    "1004 - Application-defined or object-defined error"

    Here is my code:

    Please Login or Register  to view this content.

    The line of code that contains the error is:

    Please Login or Register  to view this content.
    Hopefully you guys can debunk this bug. I have spent sometime looking online for tips as to how it could be fixed with very little luck.

    Anyways thanks for any help you provide,
    blueblazingdemon

    P.s. This is my first post ever, any tips that could improve my posts for future posts is always welcomed.
    Last edited by blueblazingdemon; 06-10-2013 at 09:48 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

  3. #3
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    Fixed! Thanks for informing me, I could not find that earlier.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    What exactly is the code meant to do?

    What's supposed to be trimmed from the database?

    One thing you should do is remove all the instances of CStr and replace + with & when you are trying to concatenate.

    For example.
    Please Login or Register  to view this content.
    Also, you should check the values of all the variables, eg k, when you get the error.

    You can do that in the Locals Window (View>Locals Window).
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    I am trying to remove rows of data that are not needed. Basically this goes through the data row by row and eliminates what is not needed based on the values of three different columns of the data. I go through the data checking one column the whole time eliminating the data based on that column requirement. Next, I have the macro go until it discovers an empty cell, then its suppose to switch its checking to the next column requirement. Then it goes back to the top and repeats itself until I have gone through each column requirement.

    I checked the local values and the one that seems really off is how large K, my counter, is, which ended up being 1048571. This is odd because the maximum numbers of rows it needs to check is 1010.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    I thought k might be the problem.

    It looks like your code is moving way past the last row of data and going all the way down to the very last row on the worksheet.

    When it gets there it can't go any further, and you'll get the 'Application defined...' error.

    I think you need to rearrange the code to prevent that happening.

    To start with try finding the last row of data before you start looping.

    Then loop from that row upwards, that's kind of standard practice when deleting.

    So the loop would look something like this.
    Please Login or Register  to view this content.
    Now within the loop you should check all 3 columns at the same time to see if the row has to be deleted.

    So, if I've got the criteria remotely right, the code would look something like this
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    thanks, that advice was helpful. I did plan on making it check the three columns at once, but waited to make sure my parameters were correct first. After rearranging my code it did remove the error, but I am having the issue that it doesn't recognize the parameters now and just deletes all the data. here is my new code after the rearrange:

    Please Login or Register  to view this content.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    Does it work for the first two criteria, without the 'PRODUCTION' criteria?

  9. #9
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    yes it works without that line

  10. #10
    Registered User
    Join Date
    06-10-2013
    Location
    Wisconsin, US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Error When Implementing Code: "1004 - Application-defined or object-defined error"

    I figured it out. The issue was when the data is pulled from the database, there is a space in front of and after the string within the cell in column J. Thus, this was making it delete every row.

    That makes it work perfectly. Thanks for all the help Norie, have a good rest of the day!

    -blueblazingdemon

+ 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