Alguien que me ayude a cambiar lógica de una macro
Buen dia
Yo hice esta macro pero me pidieron que cambiara la lógica para hacerla as optima, y me he puesto a investigar pero aun no tengo en claro como usar esos comando alguien me pudiera ayudar para hacer los cambios que me piden.... De antemano gracias
junto son el código agregue los comentarios que me pidieron cambiar
''** Si vas a usar una hoja/archivo mas de una vez asignalo a una variable. Cada que usas una directiva
''** Workbooks(carpeta).Worksheets("LOG")
''** Estas haciendo que Excel tenga que resolver el archivo/hoja al que te refieres y generar una referencia en memoria.
''** Crea una variable donde almacenes esa referencia y asi solo se tiene que resolver una sola vez
''** Dim oWS as worksheet: set oWS = Workbooks(carpeta).Worksheets("LOG")
Sub Macro1()
'
Dim strValuesSource(0 To 197) As String
Dim strValuesTarget(0 To 197) As String
Range("A7:A18").Select
''Evita usar Select, no es necesario activar celdas/rangos u objetos para manipularlos.
''Interactúa con el front-end lo menos posible ya que es sumamente costoso en recursos
Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
''**La información de origen o destino no debe ser manipulada, no es necesario que la formates para poder importarla. Excel almacena las fechas como un numero decimal,
''**la parte entera representa la fecha y los decimales la hora, revisa los detalles en linea.
''** El archivo de carga no puede quedar fijo, debes agregar en el front-end un botón que dispare el proceso de carga y presente al usuario con un dialogo para seleccionar el archivo a importar
''** usa la función de sistema Application.GetOpenFilename
'carpeta = "C:\Documents and Settings\dulcem.garcia\My Documents\GE Confidencial\LiquidFuiel\297176_1-146167321_remington gt2_20100420_liquid fuel tuning.xls"
carpeta = "297176_1-146167321_remington gt2_20100420_liquid fuel tuning.xls"
''**EVITA hacer múltiples lecturas de celdas, hacer esto es vuelve el código sumamente ineficiente. Haz UNA SOLA lectura de todo un rango y almacena los valores a un arreglo variant.
''**Dim vTemp as variant
''** vTemp = Thisworkbook.worksheets("LOG").Range("6:7").value
' Get the headers of first range
For i = 0 To 196 Step 1
strValuesSource(i) = Workbooks(carpeta).Worksheets("LOG").Range("A7").Offset(0, i)
Next i
''** Usa formas mas eficientes para hacer las búsquedas, estas accediendo una celda a la vez y para cada celda buscas todos los tags.
''** Selecciona el rango sobre el que quieres buscar y usa las funciones existentes de búsqueda. Mete los tags que estas buscando a un arreglo e itera sobre ese arreglo.
''** Workbooks(carpeta).Worksheets("LOG").Range("7:7").Find("NOX_AT_15_PCT").Column
' Get Headers of second range
For i = 0 To 196 Step 1
strValuesTarget(i) = Sheets("DATA").Range("A6").Offset(0, i)
If (strValuesTarget(i) = "Test Time") Then
strValuesTarget(i) = "TIME"
Else
If (strValuesTarget(i) = "PK1") Then
strValuesTarget(i) = "PEAK1"
Else
If (strValuesTarget(i) = "FQ1") Then
strValuesTarget(i) = "FREQ1"
Else
If (strValuesTarget(i) = "PK2") Then
strValuesTarget(i) = "PEAK2"
Else
If (strValuesTarget(i) = "FQ2") Then
strValuesTarget(i) = "FREQ2"
Else
If (strValuesTarget(i) = "PK3") Then
strValuesTarget(i) = "PEAK3"
Else
If (strValuesTarget(i) = "FQ3") Then
strValuesTarget(i) = "FREQ3"
Else
If (strValuesTarget(i) = "NOX") Then
strValuesTarget(i) = "NOx_PPM"
Else
If (strValuesTarget(i) = "O2") Then
strValuesTarget(i) = "O2_PCT"
Else
If (strValuesTarget(i) = "PWPR") Then
strValuesTarget(i) = "WIPPR"
Else
If (strValuesTarget(i) = "PLPRP") Then
strValuesTarget(i) = "FPPR"
Else
If (strValuesTarget(i) = "AATI_1A") Then
strValuesTarget(i) = "AAT"
Else
If (strValuesTarget(i) = "NOX @15") Then
strValuesTarget(i) = "NOX_AT_15_PCT"
Else
If (strValuesTarget(i) = "CO") Then
strValuesTarget(i) = "CO_PPM"
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next i
''** Lee todos los datapoints a un arreglo variant y accesa ese arreglo en memoria, no directo de la hoja.
NextRow = 1
For g = 1 To 60 Step 1
If (Workbooks(carpeta).Worksheets("LOG").Range("BS7").Offset(g, 0).Value = "Y") _
Or (Workbooks(carpeta).Worksheets("LOG").Range("BS7").Offset(g, 0).Value = "B") Then
For i = 0 To 196 Step 1
headerToFind = strValuesSource(i)
Index = -1
For j = 0 To 196 Step 1 'recorre el destino
If (headerToFind = strValuesTarget(j)) Then
Index = j
End If
Next j
If (Index > -1) Then
Sheets("DATA").Range("A6").Offset(NextRow, Index).Value = _
Workbooks(carpeta).Worksheets("LOG").Range("A7").Offset(g, i).Value
End If
Next i
NextRow = NextRow + 1
End If
Next g
End Sub