Proceso lento:Buscar y Cargar
Publicado por Catita Zarate (14 intervenciones) el 16/02/2010 12:31:59
Amigos:
La macro que estoy trabajando tiene el proceso lento
Es asi como funciona esta macro:
En la HOJA("control transporte") tengo codigos de rutas entre las columnas N:U y la fecha en U3, estos codigos y fecha se deben buscar en la base de dato HOJA("base") en la columna AE y Q, de coincidir ambos se deben sumar los kilos de la columna C y pegar en la columna X de la HOJA("control transporte").
En la columna W de la HOJA("control transporte") se deben pegar los kilos de la HOJA("BASE") de la columna A, siempre en cuando los codigos de ambas HOJAS sean iguales y la fecha de U3 sea distinto en la HOJA("base") de la columna L y la columna K sea distinto a espacio en blanco y N sea igual a espacio en blanco.
Por favor, si alguien tuviera una mejor idea como optimizarlo se le agradece
Este es el codigo:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim old&, FILAA&, FILA1&, Kilos#, FilaB&
Dim WS1 As Worksheet, WS2 As Worksheet, Col&, Fil&, Cero&, Kil#
If Target.Address(False, False) = "X4" Then
Worksheets("Control Transporte").Range("X5:X135").Value = Empty
Worksheets("Control Transporte").Range("W5:W135").Value = Empty
With Application
.ScreenUpdating = False
old = .Calculation
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set WS1 = Worksheets("Control Transporte")
Set WS2 = Worksheets("base")
FilaB = 2
While WS2.Cells(FilaB, 9).Value <> ""
FilaB = FilaB + 1
Wend
For Fil = 9 To 130
Kil = 0#
Kilos = 0#
For Col = 14 To 21
If WS1.Cells(Fil, Col).Value <> "" Then
For FILA1 = 2 To FilaB
If LCase(WS1.Cells(Fil, Col).Value) = LCase(WS2.Cells(FILA1, 31).Value) Then
If WS1.Cells(3, 21).Value = WS2.Cells(FILA1, 17).Value Then
Kilos = Kilos + WS2.Cells(FILA1, 3).Value
ElseIf WS1.Cells(3, 21).Value <> WS2.Cells(FILA1, 12).Value And WS2.Cells(FILA1, 11).Value <> Empty _
And WS2.Cells(FILA1, 14).Value = Empty Then
Kil = Kil + WS2.Cells(FILA1, 1).Value
End If
End If
Next FILA1
End If
WS1.Cells(Fil, 24).Value = Kilos
WS1.Cells(Fil, 24).Interior.ColorIndex = 36
WS1.Cells(Fil, 24).Font.ColorIndex = 5
WS1.Cells(Fil, 24).Font.Bold = True
WS1.Cells(Fil, 23).Value = Kil
WS1.Cells(Fil, 23).Interior.ColorIndex = 36
WS1.Cells(Fil, 23).Font.ColorIndex = 3
WS1.Cells(Fil, 23).Font.Bold = True
Next Col
Next Fil
'Las filas que no tengan Rutas se limpiaran para evitar la cantidad cero
For Cero = 9 To 130
If WS1.Cells(Cero, 22).Value = "" And WS1.Cells(Cero, 24).Value = "0" Then
WS1.Cells(Cero, 24).Value = ""
End If
If WS1.Cells(Cero, 22).Value = "" And WS1.Cells(Cero, 23).Value = "0" Then
WS1.Cells(Cero, 23).Value = ""
End If
Next Cero
With Application
.ScreenUpdating = True
.Calculation = old
.EnableEvents = True
End With
Set WS1 = Nothing
Set WS2 = Nothing
Range("J1").Select
With Selection.Font
.Name = "Old English Text MT"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.Font.ColorIndex = 36
ActiveCell.FormulaR1C1 = "Catalina Zarate R"
Range("J6").Select
End If
End Sub
Atte.
Catita Z. R.
La macro que estoy trabajando tiene el proceso lento
Es asi como funciona esta macro:
En la HOJA("control transporte") tengo codigos de rutas entre las columnas N:U y la fecha en U3, estos codigos y fecha se deben buscar en la base de dato HOJA("base") en la columna AE y Q, de coincidir ambos se deben sumar los kilos de la columna C y pegar en la columna X de la HOJA("control transporte").
En la columna W de la HOJA("control transporte") se deben pegar los kilos de la HOJA("BASE") de la columna A, siempre en cuando los codigos de ambas HOJAS sean iguales y la fecha de U3 sea distinto en la HOJA("base") de la columna L y la columna K sea distinto a espacio en blanco y N sea igual a espacio en blanco.
Por favor, si alguien tuviera una mejor idea como optimizarlo se le agradece
Este es el codigo:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim old&, FILAA&, FILA1&, Kilos#, FilaB&
Dim WS1 As Worksheet, WS2 As Worksheet, Col&, Fil&, Cero&, Kil#
If Target.Address(False, False) = "X4" Then
Worksheets("Control Transporte").Range("X5:X135").Value = Empty
Worksheets("Control Transporte").Range("W5:W135").Value = Empty
With Application
.ScreenUpdating = False
old = .Calculation
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set WS1 = Worksheets("Control Transporte")
Set WS2 = Worksheets("base")
FilaB = 2
While WS2.Cells(FilaB, 9).Value <> ""
FilaB = FilaB + 1
Wend
For Fil = 9 To 130
Kil = 0#
Kilos = 0#
For Col = 14 To 21
If WS1.Cells(Fil, Col).Value <> "" Then
For FILA1 = 2 To FilaB
If LCase(WS1.Cells(Fil, Col).Value) = LCase(WS2.Cells(FILA1, 31).Value) Then
If WS1.Cells(3, 21).Value = WS2.Cells(FILA1, 17).Value Then
Kilos = Kilos + WS2.Cells(FILA1, 3).Value
ElseIf WS1.Cells(3, 21).Value <> WS2.Cells(FILA1, 12).Value And WS2.Cells(FILA1, 11).Value <> Empty _
And WS2.Cells(FILA1, 14).Value = Empty Then
Kil = Kil + WS2.Cells(FILA1, 1).Value
End If
End If
Next FILA1
End If
WS1.Cells(Fil, 24).Value = Kilos
WS1.Cells(Fil, 24).Interior.ColorIndex = 36
WS1.Cells(Fil, 24).Font.ColorIndex = 5
WS1.Cells(Fil, 24).Font.Bold = True
WS1.Cells(Fil, 23).Value = Kil
WS1.Cells(Fil, 23).Interior.ColorIndex = 36
WS1.Cells(Fil, 23).Font.ColorIndex = 3
WS1.Cells(Fil, 23).Font.Bold = True
Next Col
Next Fil
'Las filas que no tengan Rutas se limpiaran para evitar la cantidad cero
For Cero = 9 To 130
If WS1.Cells(Cero, 22).Value = "" And WS1.Cells(Cero, 24).Value = "0" Then
WS1.Cells(Cero, 24).Value = ""
End If
If WS1.Cells(Cero, 22).Value = "" And WS1.Cells(Cero, 23).Value = "0" Then
WS1.Cells(Cero, 23).Value = ""
End If
Next Cero
With Application
.ScreenUpdating = True
.Calculation = old
.EnableEvents = True
End With
Set WS1 = Nothing
Set WS2 = Nothing
Range("J1").Select
With Selection.Font
.Name = "Old English Text MT"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.Font.ColorIndex = 36
ActiveCell.FormulaR1C1 = "Catalina Zarate R"
Range("J6").Select
End If
End Sub
Atte.
Catita Z. R.
Valora esta pregunta


0