+ Reply to Thread
Results 1 to 18 of 18

[SOLVED]Macro works for data range but hangs when run on data table

  1. #1
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    [SOLVED]Macro works for data range but hangs when run on data table

    I was using the code below to delete rows not equal to a cell value that was defined in the main code that calls this one.

    When using it against a normal data range it works great but when using it on a data table, it hangs at
    Please Login or Register  to view this content.
    Any help would be much appreciated.




    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by xtroublex; 11-07-2019 at 05:46 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    See the yellow banner at the top of this page …

  3. #3
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    Apologies. I have done as requested.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    A code is even not necessary just using Excel basics like a filter then delete in less than 5 seconds ! …

    And if a code is really necessary, activate the Macro Recorder and operate the same operations
    then you will get your own code base.

    For small to middle data I would use the same way within a code,
    for big data I may use a helper column, a sort then deleting all at once …

  5. #5
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    This is one macro in a longer automated process. It is designed to be completely hands off. I have tried a normal way of just Deleting rows that are <> C5 but it takes forever. This code was a great solution until I changed the data range to a table using a query to import the data. Why would it not work with a data table as opposed to a range?

    The number of records in the real table is 47k

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool


    As your start to loop instead of a filter a beginner starter :

    PHP Code: 
    Sub Macro1()
        
    Dim S$, R&
            
    Sheet7.[C5].Value2
            Application
    .ScreenUpdating False
        With Sheet5
    .ListObjects(1).ListRows
            
    For = .Count To 1 Step -1
                
    If .Item(R).Range(1).Value2 <> S Then .Item(R).Delete
            Next
        End With
            Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 11-07-2019 at 04:40 PM. Reason: optimizing the Macro Recorder code …

  7. #7
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    Thank you for that. However it doesn't have the speed that the original code (yes, the one that doesn't work now. lol) had. I will be running this on up to 20 files and it takes 3 minutes per file. That's with Calcs Screen Updating etc all switched off. This is my dilema. I cannot find a solution so far that will run as quickly as the original code. Does it make sense that the original code works with a regular data range but not a table?

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    I already saw children operating manually in less than a minute just using Excel basics (see 'big data' in post #4)
    while any bad looping code was lasting much more than 5 minutes (70K to 90K rows) …
    So the first time I changed my mind and think about trying to apply the main TEBV rule.

  9. #9
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    Let me clarify,

    3min was only running the code you provided. I do not know what TEBV is, and I'm assuming you do not know why the code I originally posted does not work with a table only because of a lack of an answer to that question.

    Thanks

  10. #10
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Macro works for data range but hangs when run on data table

    Hey xtroublex,

    I have found when running a passer to delete multiple rows, it's a lot faster to create a range in memory by using .Rows, then delete the range, maybe that would work better for you?

    That looks something like this--

    Please Login or Register  to view this content.
    Last edited by 1aaaaaaaaaaaaaaa; 11-07-2019 at 05:02 PM.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Think Excel Before VBA ‼

    An alternative to the child way (yet demonstrated in several threads within this forum) is an advanced filter (Excel Basics again)
    in order to copy only the desired rows to another place (for example in column AH) then deleting all previous columns at once
    (which is so far faster than deleting row by row) then recreating the table if necessary …

  12. #12
    Forum Contributor
    Join Date
    10-30-2019
    Location
    Bistrita, Romania
    MS-Off Ver
    2019; 365
    Posts
    231

    Re: Macro works for data range but hangs when run on data table

    Try replacing

    rng.EntireRow.Delete

    with

    rng.Delete shift:=xlUp

  13. #13
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    Quote Originally Posted by Mrrrr View Post
    Try replacing

    rng.EntireRow.Delete

    with

    rng.Delete shift:=xlUp
    No, it hangs at the new line rng.Delete shift:=xlUp

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by xtroublex View Post
    the code I originally posted does not work with a table
    As you can obviously keep that code just starting by canceling the table (back as a standard range, see UnList in VBA help)
    and at the end of the procedure recreating the table if necessary … So you already have 3 paths as all paths lead to Roma !

  15. #15
    Forum Contributor
    Join Date
    10-30-2019
    Location
    Bistrita, Romania
    MS-Off Ver
    2019; 365
    Posts
    231

    Re: Macro works for data range but hangs when run on data table

    Quote Originally Posted by xtroublex View Post
    No, it hangs at the new line rng.Delete shift:=xlUp
    Weird that it doesn't hang for me on your Example workbook...

    So for you it hangs even in that workbook?

  16. #16
    Registered User
    Join Date
    09-24-2019
    Location
    Avondale, Arizona
    MS-Off Ver
    Excel 16 64 bit
    Posts
    14

    Re: Macro works for data range but hangs when run on data table

    Quote Originally Posted by Poizhan View Post
    Hey xtroublex,

    I have found when running a passer to delete multiple rows, it's a lot faster to create a range in memory by using .Rows, then delete the range, maybe that would work better for you?

    That looks something like this--

    Please Login or Register  to view this content.
    Hangs at rng.delete

  17. #17
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro works for data range but hangs when run on data table

    Altering ListObject is going to be slower than altering standard range. As it has additional layer.

    But try following and see if it's any faster.
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  18. #18
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Macro works for data range but hangs when run on data table

    Quote Originally Posted by xtroublex View Post
    Hangs at rng.delete
    How many rows are we talking about? It could be your dataset could be managed better in a true database.

    With a database you could run a simple query "DELETE FROM table WHERE x = true"

    I'm sensing that you may be running into memory limitation issues, if you're trying to modify too many rows, if as you say it's hanging (not just throwing an error)

    ...and not memory limitation of your computer, but the Excel application engine
    Last edited by 1aaaaaaaaaaaaaaa; 11-07-2019 at 05:45 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help with macro to reformat data into table (rows must contain same date range)
    By BoyWithNoName in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2019, 11:21 AM
  2. Replies: 1
    Last Post: 07-02-2018, 07:32 PM
  3. Is it possible to create a named range who works with filtered data
    By HaroonSid in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-04-2017, 07:49 AM
  4. Macro works on Range but not on Table
    By TheNewUnion in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-02-2016, 07:46 AM
  5. Macro Pivot table where the range of data always changes
    By stimpsond1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-03-2012, 12:49 PM
  6. Macro hangs up often but sometimes works fine
    By Jeff in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-13-2006, 08:10 AM
  7. [SOLVED] Macro to update pivot table data range
    By Mike_M in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2005, 11:06 AM

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