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.
Bookmarks