+ Reply to Thread
Results 1 to 7 of 7

Thread: How can I speed up this slow macro?

  1. #1
    Registered User
    Join Date
    07-17-2008
    Location
    Kansas, USA
    Posts
    3

    How can I speed up this slow macro?

    I have a 17,000 row excel 2007 spread sheet that looks like this:

    PHP Code: 
       A         B      C       D         E         F         G        H
    DATA    2000    2003    Isuzu    Trooper     DATA    DATA    DATA
    DATA    1996    1999    Isuzu    Oasis       DATA    DATA    DATA
    DATA    2003    2004    Isuzu    Ascender    DATA    DATA    DATA 

    I want it to look more like this:

    PHP Code: 
       A         B      C       D         E         F         G        H
    DATA    2000            Isuzu    Trooper     DATA    DATA    DATA
    DATA    2001            Isuzu    Trooper     DATA    DATA    DATA
    DATA    2002            Isuzu    Trooper     DATA    DATA    DATA
    DATA    2003            Isuzu    Trooper     DATA    DATA    DATA
    DATA    1996            Isuzu    Oasis       DATA    DATA    DATA
    DATA    1997            Isuzu    Oasis       DATA    DATA    DATA
    DATA    1998            Isuzu    Oasis       DATA    DATA    DATA
    DATA    1999            Isuzu    Oasis       DATA    DATA    DATA
    DATA    2003            Isuzu    Ascender    DATA    DATA    DATA
    DATA    2004            Isuzu    Ascender    DATA    DATA    DATA 
    PHP Code: 
    Here is the macro I created to do this:

    Dim year1 As Integer
    Dim year2 
    As Integer
    Dim counter 
    As Integer
    Range
    ("B1").Select

    Do While Not IsEmpty(ActiveCell)

    ActiveCell.Offset(10).Select
    year1 
    ActiveCell.Value
    year2 
    ActiveCell.Offset(01).Value
    counter 
    year2 year1
    ActiveCell
    .Offset(01).Value ""

    Do While counter 0
    ActiveCell
    .EntireRow.Select
    Application
    .CutCopyMode False
    Selection
    .Copy
    ActiveCell
    .Offset(10).Select
    ActiveCell
    .EntireRow.Select
    Selection
    .Insert Shift:=xlDown
    ActiveCell
    .Offset(01).Select
    ActiveCell
    .Value ActiveCell.Value 1
    counter 
    counter 1
    Loop

    Loop
    End Sub 

    The problem is that after the first 100 or so lines the macro slows down to 1 line every 1 second. After 400 lines is slows to 1 line every 2 seconds. I am guessing that I will have around 300,000 lines after everything is said and done. At the rate this thing is running it will take a year to finish. Is there a better way to acomplish what I need?

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359
    Hi

    See if this is any faster.

    Sub aaa()
      For i = Cells(Rows.Count, 1).End(xlUp).Row + 1 To 2 Step -1
        cntr = Cells(i - 1, 3).Value - Cells(i - 1, 2).Value
        Cells(i, 1).Resize(cntr, 1).EntireRow.Insert shift:=xlDown
        Cells(i, 1).Resize(cntr, 1).Value = Cells(i - 1, 1).Value
        Cells(i, 4).Resize(cntr, 5).Value = Cells(i - 1, 4).Resize(1, 5).Value
        For j = 0 To cntr - 1
          Cells(i + j, 2).Value = Cells(i - 1, 2).Value + j + 1
        Next j
      Next i
      Range("C:C").ClearContents
    End Sub
    rylo

  3. #3
    Valued Forum Contributor
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2010
    Posts
    1,091
    Sub test()
    Dim a, b(), i As Long, ii As long, iii As Long, n As Long
    a = Range("a1").CurrentRegion.Value
    ReDim b(1 To Rows.Count, 1 To UBound(a,2))
    For i = 1 To UBound(a,1)
        For ii = a(i,2) To a(i,3)
            n = n + 1
            For iii = 1 To UBound(a,1)
                If iii <> 3 Then b(n,iii) = a(i, IIf(iii=2, ii, iii))
            Next
        Next
    Next
    Range("a1").Resize(n, UBound(b,2)).Value = b
    End Sub

  4. #4
    Registered User
    Join Date
    07-17-2008
    Location
    Kansas, USA
    Posts
    3
    Quote Originally Posted by rylo
    Hi

    See if this is any faster.

    Sub aaa()
      For i = Cells(Rows.Count, 1).End(xlUp).Row + 1 To 2 Step -1
        cntr = Cells(i - 1, 3).Value - Cells(i - 1, 2).Value
        Cells(i, 1).Resize(cntr, 1).EntireRow.Insert shift:=xlDown
        Cells(i, 1).Resize(cntr, 1).Value = Cells(i - 1, 1).Value
        Cells(i, 4).Resize(cntr, 5).Value = Cells(i - 1, 4).Resize(1, 5).Value
        For j = 0 To cntr - 1
          Cells(i + j, 2).Value = Cells(i - 1, 2).Value + j + 1
        Next j
      Next i
      Range("C:C").ClearContents
    End Sub
    rylo

    Thank you,

    I tried this out and got the following error:

    Run-time error '1004':

    Application-defined or object-defined error.

    Any ideas?

  5. #5
    Registered User
    Join Date
    07-17-2008
    Location
    Kansas, USA
    Posts
    3
    Quote Originally Posted by jindon
    Sub test()
    Dim a, b(), i As Long, ii As long, iii As Long, n As Long
    a = Range("a1").CurrentRegion.Value
    ReDim b(1 To Rows.Count, 1 To UBound(a,2))
    For i = 1 To UBound(a,1)
        For ii = a(i,2) To a(i,3)
            n = n + 1
            For iii = 1 To UBound(a,1)
                If iii <> 3 Then b(n,iii) = a(i, IIf(iii=2, ii, iii))
            Next
        Next
    Next
    Range("a1").Resize(n, UBound(b,2)).Value = b
    End Sub

    Thank you,

    I tried this one out and received the following error:

    Run-time error '7':

    Out of memory


    If it makes any difference I running this on a dual XEON machine with 2 GB of RAM with 32 bit XP.

  6. #6
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359
    Hi

    I put your example data into the range A1:H3, then copied it down until row 10000.

    Ran my code, and it didn't give any errors, and completed in about 1 min.

    How about you put up an example workbook, with data that causes an error. Make sure it is in 2003 format, not 2007

    rylo

  7. #7
    Valued Forum Contributor
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2010
    Posts
    1,091
    try
    It should finish within a second.
    Sub test()
    Dim a, b(), i As Long, ii As long, iii As Long, n As Long
    a = Range("a1").CurrentRegion.Resize(,8).Value
    ReDim b(1 To 30000, 1 To 8)
    For i = 1 To UBound(a,1)
        For ii = a(i,2) To a(i,3)
            n = n + 1
            For iii = 1 To UBound(a,1)
                If iii <> 3 Then b(n,iii) = a(i, IIf(iii=2, ii, iii))
            Next
        Next
    Next
    Range("a1").Resize(n, UBound(b,2)).Value = b
    End Sub

+ 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.2.0