+ Reply to Thread
Results 1 to 9 of 9

My current macro takes 20 minutes + to run. How can this code be made more efficient??!!

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2012
    Location
    chicago, illinois
    MS-Off Ver
    Excel 2007
    Posts
    5

    My current macro takes 20 minutes + to run. How can this code be made more efficient??!!

    Hello VBA pro's! The code below runs REALLY slow. I was assisted on this forum before w/ a similar problem & was hoping someone out there could help.

    Does this run so slow b/c it's not being run as a dynamic range? Variant Data? I would like to understand the logic behind why this runs as inefficiently as it does!


    Private Sub CommandButton1_Click()
    'Fill In Columns for CSV
    
    Dim intStoreNumberRow As Integer
    Dim intQuantityRow As Integer
    Dim intStoreNumberColumn As Integer
    Dim intQuantityColumn As Integer
    Dim intSheet4Row As Integer
    Dim intSheet4Counter As Integer
    
    'First Row that Data Should Appear
    intSheet4Row = 2
    intSheet4Counter = 1
    
    'Store Numbers are stored in F1:GR40
    For intStoreNumberColumn = 6 To 200
        For intStoreNumberRow = 1 To 40
            'Quantities are stored in F43:GR500
            For intQuantityRow = 43 To 340
                'If there is a Store Number and Quantity, then proceeds to fill
                If Sheet3.Cells(intStoreNumberRow, intStoreNumberColumn) <> "" And Sheet3.Cells(intQuantityRow, intStoreNumberColumn) <> 0 Then
                    'Increases Counter by 1
                    intSheet4Counter = intSheet4Counter + 1
                    'My Company - stays same
                    Sheet4.Cells(intSheet4Counter, 1) = Sheet3.Cells(1, 2)
                    'My Division - stays same
                    Sheet4.Cells(intSheet4Counter, 2) = Sheet3.Cells(2, 2)
                    'Corporation - stays same
                    Sheet4.Cells(intSheet4Counter, 3) = Sheet3.Cells(4, 2)
                    'Sold To/Company - stays same
                    Sheet4.Cells(intSheet4Counter, 4) = Sheet3.Cells(5, 2)
                    'Store Number - changes
                    Sheet4.Cells(intSheet4Counter, 5) = Sheet3.Cells(intStoreNumberRow, intStoreNumberColumn)
                    'UPC Code - changes
                    Sheet4.Cells(intSheet4Counter, 6) = Sheet3.Cells(intQuantityRow, 5)
                    'Order Type - stays same
                    Sheet4.Cells(intSheet4Counter, 7) = "RPL"
                    'Promotion Code - stays same
                    Sheet4.Cells(intSheet4Counter, 8) = "..."
                    'PO Number - stays same
                    Sheet4.Cells(intSheet4Counter, 9) = Sheet3.Cells(8, 2)
                    'Wholesale Price - stays same
                    Sheet4.Cells(intSheet4Counter, 10) = Sheet3.Cells(11, 2)
                    'Retail Price - stays same
                    Sheet4.Cells(intSheet4Counter, 11) = Sheet3.Cells(13, 2)
                    'Start Date - stays same
                    Sheet4.Cells(intSheet4Counter, 12) = Sheet3.Cells(9, 2)
                    'Cancel Date - stays same
                    Sheet4.Cells(intSheet4Counter, 13) = Sheet3.Cells(10, 2)
                    'Department Number -stays same
                    Sheet4.Cells(intSheet4Counter, 14) = Sheet3.Cells(7, 2)
                    'Replenishment Order Units - changes
                    Sheet4.Cells(intSheet4Counter, 15) = Sheet3.Cells(intQuantityRow, intStoreNumberColumn)
                End If
            Next intQuantityRow
        Next intStoreNumberRow
    Next intStoreNumberColumn
    
    
    'Format Info
        Columns("A:A").Select
        Selection.NumberFormat = "00"
        Columns("B:B").Select
        Selection.NumberFormat = "00"
        Columns("C:D").Select
        Selection.NumberFormat = "General"
        Columns("E:E").Select
        Selection.NumberFormat = "0000"
        Columns("F:F").Select
        Selection.NumberFormat = "000000000000"
        Columns("G:H").Select
        Selection.NumberFormat = "General"
        Columns("J:K").Select
        Selection.NumberFormat = "0.00"
        Columns("L:M").Select
        Selection.NumberFormat = "mm/dd/yyyy"
        Columns("N:N").Select
        Selection.NumberFormat = "000"
        Columns("O:O").Select
        Selection.NumberFormat = "00000000000"
    
    End Sub


    Thanks in advance!!!!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    I believe your code is looping 2.3 million times.
    Post a very simple sample workbook showing the original layout of the workbook and what the desired results should be. Only make the sample workbook 20 rows or so.
    Make the workbook only show sample data.

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Hi

    Have you considered the amount of data you are processing here? 3 loops - 195, 40 and 298. Multiplying these comes to over 2.3m items to process.

    I don't know how many of those will pass the test and be processed, but either way, there is a fair chunk of data to work through.

    rylo

  4. #4
    Registered User
    Join Date
    03-08-2012
    Location
    chicago, illinois
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Hi Rylo & Dave-



    I've attached the workbook w/ sample data to give you a better idea. I THINK the problem is that the code automatically loops 2.3 million times REGARDLESS of the actual parameters of the data in question. For example, in the "assortment" tab the sample data stops @ row 48 (not 340) & column 118 (not 195).



    Note that the code that's in question is on the "info for .csv" tab. In order for you to use that macro however you need to first use the one on the "assortment info" tab.



    Thanks for all your help!!!
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Private Sub CommandButton2_Click()
    'Clear Columns
    
    'Dim intSheet4InfoColumn As Integer
    'Dim lngSheet4InfoRow As Long
    
    'For intSheet4InfoColumn = 1 To 40
    '    For lngSheet4InfoRow = 2 To 65536
    '        Sheet4.Cells(lngSheet4InfoRow, intSheet4InfoColumn).Clear
    '    Next lngSheet4InfoRow
    'Next intSheet4InfoColumn
    Sheet4.Cells.Clear
    
    
    End Sub
    The above is a better way to clear a worksheet. As for the rest, I honestly don't know what you are doing. Maybe someone else comes along but in the meantime explain in english what you are doing?

    Thanks.

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Hi

    For the 2 loop ends that you have mentioned, why don't you work out the current loop ends rather than have them fixed.

    The 48 (last row) can be determined by
    cells(rows.count,"B").end(xlup).row
    and the last column by
    cells(42,columns.count).end(xltoleft).column
    This will at least reduce the volume of data being processed.

    The third loop (currently 1 - 40) could be made dynamic by something like
    cells(41,1).end(xlup).row
    Again, that will further reduce your code processing.

    HTH

    rylo

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Hi Mr.Wiskers,

    Please explain in a point by point format what your code is supposed to do
    • If this equals this then
    • somthing =this
    • and something else = this.
    Do not be to general, we need the information.
    Like 111StepsAhead says, you do not need to loop through cells to delete them.
    His example to clear the sheet and this one for the other sheet.

    Private Sub CommandButton2_Click()
    'Clear Columns
    '
    'Dim intSheet4InfoColumn As Integer
    'Dim lngSheet4InfoRow As Long
    '
    'For intSheet4InfoColumn = 1 To 40
    '    For lngSheet4InfoRow = 2 To 65536
    '        Sheet4.Cells(lngSheet4InfoRow, intSheet4InfoColumn).Clear
    '    Next lngSheet4InfoRow
    'Next intSheet4InfoColumn
    
    Range("A2:O65536").Clear ' or whatever range you want to clear.
    Do not let this thread drag on and on......

  8. #8
    Registered User
    Join Date
    03-08-2012
    Location
    chicago, illinois
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    Okay -

    Given the format in tab 1 ("Assortment"), the first macro in this workbook in tab 2 ("Assortment info") is needed to remove the formatting from tab 1 as well as the subtotals.

    What I need the 2nd macro to do in tab 3 ("Info for csv") is to take the data from tab 2 ("Assortment info" and put it into the form of individual records.

    For example,

    all records will have the same info in the top left of tab 2:
    SAMPLE COMPANY: 02
    SAMPLE DIVISION: 05
    BRAND: SAMPLE
    CORPORATION: SAMPLE
    ACCOUNT NAME: SAMPLE
    ACCOUNT #: 0012345
    ACCOUNT DEPT: 123
    PO #: 1234567
    START DATE: 03/22/2012
    CANCEL DATE: 04/02/2012
    COST: $1.00
    OWNED PRICE: $2.00
    TICKET PRICE: $3.00

    • Each record needs to have only 1 store # associated w/ it (this is the data in tab2 rows 1-40 in columns f-ds)
    • Each record/door needs to show all unique "Item/SKU (UPC)s" associated w/ it.
    • The ONLY fields that change are columns E/F/O

    Thanks!

  9. #9
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: My current macro takes 20 minutes + to run. How can this code be made more efficient?

    This is commandbutton 1 click updated.

    Private Sub CommandButton1_Click()
    'Fill In Columns for CSV
    
    Dim intStoreNumberRow As Integer
    Dim intQuantityRow As Integer
    Dim intStoreNumberColumn As Integer
    Dim intQuantityColumn As Integer
    Dim intSheet4Row As Integer
    Dim intSheet4Counter As Integer
    
    Dim last_Row As Integer
    Dim x As Integer
    
    'last_Row = Range(Cells(1, intStoreNumberColumn), Cells(40, intStoreNumberColumn)).End(xlUp).Row
    
    'First Row that Data Should Appear
    intSheet4Row = 2
    intSheet4Counter = 1
    
    'Store Numbers are stored in F1:GR40
    For intStoreNumberColumn = 6 To 200
        last_Row = Range(Cells(1, intStoreNumberColumn), Cells(40, intStoreNumberColumn)).End(xlUp).Row
        For x = 1 To last_Row
            'Quantities are stored in F43:GR500
            For intQuantityRow = 43 To 47
                'If there is a Store Number and Quantity, then proceeds to fill
                If Sheet3.Cells(x, intStoreNumberColumn) <> "" And Sheet3.Cells(intQuantityRow, intStoreNumberColumn) <> 0 Then
                    'Increases Counter by 1
                    intSheet4Counter = intSheet4Counter + 1
                    'Randa Company - stays same
                    Sheet4.Cells(intSheet4Counter, 1) = Sheet3.Cells(1, 2)
                    'Randa Division - stays same
                    Sheet4.Cells(intSheet4Counter, 2) = Sheet3.Cells(2, 2)
                    'Corporation - stays same
                    Sheet4.Cells(intSheet4Counter, 3) = Sheet3.Cells(4, 2)
                    'Sold To/Company - stays same
                    Sheet4.Cells(intSheet4Counter, 4) = Sheet3.Cells(5, 2)
                    'Store Number - changes
                    Sheet4.Cells(intSheet4Counter, 5) = Sheet3.Cells(x, intStoreNumberColumn)
                    'UPC Code - changes
                    Sheet4.Cells(intSheet4Counter, 6) = Sheet3.Cells(intQuantityRow, 5)
                    'Order Type - stays same
                    Sheet4.Cells(intSheet4Counter, 7) = "RPL"
                    'Promotion Code - stays same
                    Sheet4.Cells(intSheet4Counter, 8) = "..."
                    'PO Number - stays same
                    Sheet4.Cells(intSheet4Counter, 9) = Sheet3.Cells(8, 2)
                    'Wholesale Price - stays same
                    Sheet4.Cells(intSheet4Counter, 10) = Sheet3.Cells(11, 2)
                    'Retail Price - stays same
                    Sheet4.Cells(intSheet4Counter, 11) = Sheet3.Cells(13, 2)
                    'Start Date - stays same
                    Sheet4.Cells(intSheet4Counter, 12) = Sheet3.Cells(9, 2)
                    'Cancel Date - stays same
                    Sheet4.Cells(intSheet4Counter, 13) = Sheet3.Cells(10, 2)
                    'Department Number -stays same
                    Sheet4.Cells(intSheet4Counter, 14) = Sheet3.Cells(7, 2)
                    'Replenishment Order Units - changes
                    Sheet4.Cells(intSheet4Counter, 15) = Sheet3.Cells(intQuantityRow, intStoreNumberColumn)
                End If
            Next intQuantityRow
        Next x
    Next intStoreNumberColumn
    
    
    'Format Info
        Columns("A:A").Select
        Selection.NumberFormat = "00"
        Columns("B:B").Select
        Selection.NumberFormat = "00"
        Columns("C:D").Select
        Selection.NumberFormat = "General"
        Columns("E:E").Select
        Selection.NumberFormat = "0000"
        Columns("F:F").Select
        Selection.NumberFormat = "000000000000"
        Columns("G:H").Select
        Selection.NumberFormat = "General"
        Columns("J:K").Select
        Selection.NumberFormat = "0.00"
        Columns("L:M").Select
        Selection.NumberFormat = "mm/dd/yyyy"
        Columns("N:N").Select
        Selection.NumberFormat = "000"
        Columns("O:O").Select
        Selection.NumberFormat = "00000000000"
    
    End Sub
    This is based on the what "I think" you are trying to do. First of all I don't loop from 1 to 40. I loop until the first blank appears. So that cuts down some time. Secondly, your very inner loop doesn't make sense.

    You loop from 43-340 but the values you copy are within rows 43-47.

    *** the values you are testing are within rows 43-47.

    Anyways, replace the above code and tell me what happens or if I misunderstood what you are doing.

    Thanks!

    edit: In hindsight maybe it is 43-340 because you have more rows in the real version.
    Last edited by 111StepsAhead; 04-11-2012 at 11:10 AM.

+ 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