+ Reply to Thread
Results 1 to 4 of 4

Deleting and resizing in a large data set?

  1. #1
    Registered User
    Join Date
    02-05-2013
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Deleting and resizing in a large data set?

    Hoping someone can offer up some feedback on this one.

    I have a large data set of 100000 lines where I am auto-filtering based on predefined criteria. From here, I am copying the data matching the criteria to another workbook; and then deleting the data from from the original workbook as well.

    FYI, I have no real experience with VB, everything I've learned is purely from the net so please forgive my errors syntax, etc.
    Here is my code:

    Sub TestScript()
    Sheets("Successful").Select
    Dim bRow As Long, rVis As Range, rData As Range, rCount As Long

    With Sheets("Successful")
    .Range("A1").AutoFilter
    Rows("1:1").Select
    ActiveWorkbook.Sheets("Successful").AutoFilter.Sort.SortFields.Clear
    bRow = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A1:A" & bRow).AutoFilter Field:=9, Criteria1:=Array("=#N/A", "=0"), Operator:=xlFilterValues
    Set rData = Worksheets("Successful").UsedRange
    Set rVis = rData.SpecialCells(xlCellTypeVisible)
    rCount = WorksheetFunction.Count(rVis.Cells.SpecialCells(xlCellTypeVisible))
    rVis.Copy Destination:=Worksheets("Risk-NoStockState").Range("A" & Rows.Count).End(xlUp).Offset(0)
    Application.DisplayAlerts = False
    If rCount <> 0 Then ' ensures the code below will only activate if there is a row to delete!
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    End If
    Application.DisplayAlerts = True
    .AutoFilterMode = False
    End With
    End Sub

    The code works fine, but on large data sets, the code line "ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete" takes for ever to complete and I don't quite understand why.
    I've tried various things such as setting the calculations to manual (Application.Calculation = xlCalculationManual), but it doesn't seem to make a difference.
    Can someone tell me if there is a more efficient way to rewrite this line or my code to make it more efficient on larger data sets?
    Any useful feedback is greatly appreciated!!
    Thanks
    Steve

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Deleting and resizing in a large data set?

    Your code isn't that bad for a self-proclaimed noob. Well done.

    Deleting many non-contiguous rows can take a lot of time. This is a known issue. One method to make it faster is to sort your filtered data so they are one contiguous block of rows and then delete them.

    After you autofilter the data:
    1.) In an empty column, put a value (any value) for the filtered rows you want to delete.
    2.) Sort on that column (sorting is fast). That will put all the rows you want to delete in one contiguous block.
    3.) Delete the rows.

    Example: uses column Z as the temporary helper column to sort on.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    02-05-2013
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Deleting and resizing in a large data set?

    Hi, and thanks for your prompt response.
    I tested your code above over night on 100,000 lines of data.
    I found a significant reduction in the script running time.
    It went from around 1.5 hours to about 20 minutes (I have many subs that run similarly and I used this in all of them)
    Thank-you so much for your feedback as its really helped me to fix this annoying issue - this is nothing short of spectacular!!
    My original data set was 500,000 lines which I admit was a little adventurous, but 20 minutes for 100,000 is fantastic for what I'm trying to achieve - its way better than I was ever hoping for.
    Regards
    Steve

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Deleting and resizing in a large data set?

    You're welcome. Glad it worked.

+ 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. Deleting large numbers of rows
    By dchun in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-04-2013, 03:53 PM
  2. [SOLVED] Deleting large amount of shapes
    By Miroslav R. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-17-2013, 04:08 AM
  3. Replies: 5
    Last Post: 11-14-2011, 04:48 PM
  4. Deleting and resizing the calendar objects
    By Bobmckelvy in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 09-16-2006, 08:31 PM
  5. Resizing cells in a selection without resizing entire sheet
    By Danielle via OfficeKB.com in forum Excel General
    Replies: 4
    Last Post: 08-11-2006, 05:10 PM

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