Ok suponemos que tenemos una hoja con datos así en A a C:
<!-- Please do not remove this header --><!-- Table easily created from Excel with ASAP Utilities (
http://www.asap-utilities.com) --><table border="1" bordercolor="#C0C0C0" bordercolordark="#FFFFFF" cellspacing="0"><tr><td bgcolor="#000000" width="147" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Número de Artículo</b></font></td><td bgcolor="#000000" width="120" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Precio</b></font></td><td bgcolor="#000000" width="69" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Inventario</b></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">A-1232-456-A</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">45</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">251</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">1-222-333-41</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">18</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">118</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">5-147-198-44</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">11</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">94</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">J-9987-135-X</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">122</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">48</font></td></tr></table>
Y queremos que VBA agregue una columna con el valor del inventario. Tenemos dos opciones, una con usar .FORMULA y otra con usar .FORMULAR1C1. Si estamos convencidos que jamás nadie va a cambiar la estructura de la hoja no importa. Ambas opciones nos da igual.
Code:
Sub InsertarValorInvent_A1()
Dim rngCurr As Excel.Range, rngTarget As Excel.Range
'// para ser más sencilla usamos la hoja activa
Set rngCurr = Range("A1").CurrentRegion
With rngCurr
Set rngTarget = .Offset(, .Columns.Count).Resize(1, 1)
.Range("A1").Copy
End With
With rngTarget
.PasteSpecial xlPasteFormats
.Value = "Valor Invent"
Set rngTarget = .Offset(1).Resize(rngCurr.Rows.Count - 1, 1)
End With
With rngTarget
'// using .Formula
.Formula = "=B2*C2"
.NumberFormat = "#,000"
.EntireColumn.ColumnWidth = 16
End With
End Sub
Sub InsertarValorInvent_R1C1()
Dim rngCurr As Excel.Range, rngTarget As Excel.Range
'// para ser más sencilla usamos la hoja activa
Set rngCurr = Range("A1").CurrentRegion
With rngCurr
Set rngTarget = .Offset(, .Columns.Count).Resize(1, 1)
.Range("A1").Copy
End With
With rngTarget
.PasteSpecial xlPasteFormats
.Value = "Valor Invent"
Set rngTarget = .Offset(1).Resize(rngCurr.Rows.Count - 1, 1)
End With
With rngTarget
'// using .FormulaR1C1
.FormulaR1C1 = "=RC[-2]*RC[-1]"
.NumberFormat = "#,000"
.EntireColumn.ColumnWidth = 16
End With
End Sub
PERO, si en una semana llega el jefe y nos dice «¿mira, ese reporte que automatizó usted la semana pasada? Bueno dije a informática que ponga otra columna. ¿Eso no afectará su macro, verdad que no?» Y la respuesta tiene que ser «depende». Porque si usted usó .FORMULA y puso la columna al final, no hay problema, pero si usted usó .FORMULA e insertaron la columna en B o C entonces sí hay problema. Mientras si usó .FORMULAR1C1 falla si metieron a la derecha pero funciona si metieron a la izquierda o sea:
<!-- Please do not remove this header --><!-- Table easily created from Excel with ASAP Utilities (
http://www.asap-utilities.com) --><table border="1" bordercolor="#C0C0C0" bordercolordark="#FFFFFF" cellspacing="0"><tr><td bgcolor="#000000" width="147" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Número de Artículo</b></font></td><td bgcolor="#000000" width="120" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Precio</b></font></td><td bgcolor="#000000" width="69" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Inventario</b></font></td><td bgcolor="#953735" width="116" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Columna Nueva</b></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">A-1232-456-A</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">45</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">251</font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">5</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">1-222-333-41</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">18</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">118</font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">10</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">5-147-198-44</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">11</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">94</font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">15</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">J-9987-135-X</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">122</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">48</font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">20</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#215867" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Base de Solución</b></font></td><td bgcolor="#215867" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Resultado</b></font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">.FORMULA</font></td><td bgcolor="#FFFFFF" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">FUNCIONA</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">.FORMULAR1C1</font></td><td bgcolor="#FFFFFF" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">FALLA</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="116" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr></table>
<!-- Please do not remove this header --><!-- Table easily created from Excel with ASAP Utilities (
http://www.asap-utilities.com) --><table border="1" bordercolor="#C0C0C0" bordercolordark="#FFFFFF" cellspacing="0"><tr><td bgcolor="#000000" width="147" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Número de Artículo</b></font></td><td bgcolor="#953735" width="120" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Columna Nueva</b></font></td><td bgcolor="#000000" width="69" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Precio</b></font></td><td bgcolor="#000000" width="104" height="25" align="center" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Inventario</b></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">A-1232-456-A</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">5</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">45</font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">251</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">1-222-333-41</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">10</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">18</font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">118</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">5-147-198-44</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">15</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">11</font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">94</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">J-9987-135-X</font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">20</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">122</font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">48</font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="120" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#215867" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Base de Solución</b></font></td><td bgcolor="#215867" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#FFFFFF"><b>Resultado</b></font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">.FORMULA</font></td><td bgcolor="#FFFFFF" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">FALLA</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr><tr><td bgcolor="#FFFFFF" width="147" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">.FORMULAR1C1</font></td><td bgcolor="#FFFFFF" width="120" height="25" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000">FUNCIONA</font></td><td bgcolor="#FFFFFF" width="69" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td><td bgcolor="#FFFFFF" width="104" height="25" align="right" valign="bottom" style="white-space: nowrap"><font face="Tahoma" size="2" color="#000000"></font></td></tr></table>
Entonces ¿qué será una solución que sobrevive ambos? No queremos andar busque que busque cada línea que hace una fórmula si hay centenares o miles de líneas de código. <o
></o
>
La solución es hacemos una enumeración de la posición de las columnas y usamos R1C1.<o
></o
>
Solución original (tres columnas)
Code:
Private Enum me_Columnas
colNumeroArticulo = 1 '// by default first item is a 0, so need to redefine
colPrecio
colInventario
End Enum
Sub InsertarValorInvent_Enumerated()
Dim rngCurr As Excel.Range, rngTarget As Excel.Range, _
f As String, lngTargCol As Long
'// para ser más sencilla usamos la hoja activa
Set rngCurr = Range("A1").CurrentRegion
With rngCurr
Set rngTarget = .Offset(, .Columns.Count).Resize(1, 1)
.Range("A1").Copy
End With
With rngTarget
.PasteSpecial xlPasteFormats
.Value = "Valor Invent"
Set rngTarget = .Offset(1).Resize(rngCurr.Rows.Count - 1, 1)
End With
Let lngTargCol = rngTarget.Column
Let f = "=RC[" & me_Columnas.colPrecio - lngTargCol _
& "]*RC[" & me_Columnas.colInventario - lngTargCol & "]"
With rngTarget
'// using .FormulaR1C1
.FormulaR1C1 = f
.NumberFormat = "#,000"
.EntireColumn.ColumnWidth = 16
End With
End Sub
Si meten la nueva en B, editamos las enumeraciones así
Code:
Private Enum me_Columnas
colNumeroArticulo = 1 '// by default first item is a 0, so need to redefine
colNueva
colPrecio
colInventario
End Enum
Y si la meten en D, editamos así.
Code:
Private Enum me_Columnas
colNumeroArticulo = 1 '// by default first item is a 0, so need to redefine
colPrecio
colInventario
colNueva
End Enum
En ambos casos solo tenemos que editar las enumeraciones en la cabeza del módulo y ya, finito.
No tenemos que editar una sola letra de la rutina que escribe la fórmula.