Results 1 to 3 of 3

Optimize macro by avoiding using for statements

Threaded View

  1. #1
    Registered User
    Join Date
    10-22-2015
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    15

    Optimize macro by avoiding using for statements

    Hello everyone, I have read that using for statements makes a macro slow. I found this to be true because in my worksheet, I want to work with a large database in excel and I use for to loop through it. When the number of rows is big, it can take up to 15min. Anyone can suggest me better ways to do this?

    The following part of the code tells me where the data starts and where it ends in the specifed sheet:
    p = 1
        celdainicial = Sheets("tabla").Range("M" & p)
        Do While celdainicial <> "Tipo de orden"
            p = p + 1
            celdainicial = Sheets("tabla").Range(" M" & p) 'where the range starts
        Loop
        
        q = p + 1
        p = p + 1
        celdafinal = Sheets("tabla").Range("M" & q)
        
        Do While celdafinal <> ""
           q = q + 1
           celdafinal = Sheets("tabla").Range("M" & q)    'where the range ends
        Loop
        
        q = q - 1
        celdafinal = Sheets("tabla").Range("M" & q)
    after I get from where my range starts and where it ends, I work with it. The following code is just an example of what I want to do just to give you the idea

    for x=p to q
       ctt = Sheets("tabla").Range("N" & x)
       for y=x+1 to q
          ctt = Sheets("tabla").Range("N" & x)
       Next
    
    Next
    there's more code between that, but the goal is to start with the first data in range N&x, from there, search in the rest of the range until "q" for cells that match the data in range N&x. The complete code is the following for more details:

    For x = p To q
                    If Sheets("tabla").Range("BM" & x) = "" Then  'after each loop, it marks the row with "ya", this means the info in that row has been used                  
                        celdapiv = Sheets("tabla").Range("K" & x)                         'then it keeps looping until it finds data untouched, id like to optimize this also
                        cbt = Sheets("tabla").Range("F" & x)
                        If Sheets("tabla").Range("R" & x) = "BBL" Then 
                            cvol = Sheets("tabla").Range("Q" & x)  
                        ElseIf Range("R" & x) = "TM" Then
                            cvol = Sheets("tabla").Range("Q" & x) & "TM" 
                        End If
                        cmov = Sheets("tabla").Range("N" & x)
                        cinc = Sheets("tabla").Range("O" & x)
                        cpue = Sheets("tabla").Range("U" & x)
                    
                        If cmes = mesop And cprd = prdt Then
                            If celdapiv <> ctt Then
                                Sheets("tabla").Range("BM" & x) = "ya"     'its like a mark which indicates the data in this row has been used, so in the next loop, the data here is ignored. Id like to improve this
                                ctt = celdapiv
                                Sheets("Reporte de barcos").Range("A" & a + 3) = cbt
                            
                                a = a + 3
                                
                                For y = x + 1 To q
                                    If Sheets("tabla").Range("BG" & y) = "" Then
                                         cobs = Sheets("tabla").Range("AN" & y)
                                        
                                         If celdapiv = ctt Then
                                             Sheets("Reporte de barcos").Range("A" & a + 3) = cordcom
                                             Sheets("Reporte de barcos").Range("B" & a + 3) = cvol
                                           
                                             If cinvent = "" Then
                                                 Sheets("Reporte de barcos").Range("F" & a + 3) = ""
                                             Else: Sheets("Reporte de barcos").Range("F" & a + 3) = cinvent & " y " & cfinvent
                                             End If
                                             
                                             If czarp = "" Then
                                                 Sheets("Reporte de barcos").Range("O" & a + 3) = cestatus
                                             Else: Sheets("Reporte de barcos").Range("O" & a + 3) = czarp
                                             End If
                                             
                                             Sheets("Reporte de barcos").Range("P" & a + 3) = cvolt
                                             Sheets("Reporte de barcos").Range("Q" & a + 3) = cvolbt
                                             Sheets("Reporte de barcos").Range("R" & a + 3) = ""
                                             Sheets("Reporte de barcos").Range("S" & a + 3) = cobs
                                             
                                           
                                             a = a + 2
                                             
                                             Sheets("tabla").Range("BM" & y) = "ya"
                                             
                                         
                                         End If
                                    End If
                                Next
                                
                                
                            End If
                            
                        End If
                    End If
                    
                Next
                a = a + 2
            Next

    I hope everything is clear, i'm pretty sure there are better ways to write that code but i'm new in programming, also the code above only has the essential things id like to improve, which are the for staments.
    Last edited by alexholly; 12-15-2015 at 01:05 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Avoiding Nested If statements
    By TimEd in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-30-2012, 09:11 PM
  2. Replies: 4
    Last Post: 09-26-2012, 01:15 AM
  3. Need to optimize the small macro
    By Carnifex930 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-11-2011, 09:00 PM
  4. Avoiding "If" statements in macro
    By Myrmecophaga in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2008, 03:23 AM
  5. Optimize simple macro
    By Biff in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-06-2005, 01:05 AM
  6. Optimize simple macro
    By Biff in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-05-2005, 11:05 PM
  7. Optimize simple macro
    By Biff in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-05-2005, 10:05 PM
  8. Optimize simple macro
    By Biff in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-01-2005, 09:05 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