21.12.2012 Views

Programación Visual Basic (VBA) para Excel y Análisis ... - TEC-Digital

Programación Visual Basic (VBA) para Excel y Análisis ... - TEC-Digital

Programación Visual Basic (VBA) para Excel y Análisis ... - TEC-Digital

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>VBA</strong> <strong>para</strong> <strong>Excel</strong> M.Sc. Walter Mora F., M.Sc. José Luis Espinoza B. 62<br />

cis(3, 3) = 0.5555555556<br />

cis(4, 1) = 0.3478548451<br />

cis(4, 2) = 0.6521451549<br />

cis(4, 3) = 0.6521451549<br />

cis(4, 4) = 0.3478548451<br />

cis(5, 1) = 0.236926885<br />

cis(5, 2) = 0.4786286705<br />

cis(5, 3) = 0.5688888889<br />

cis(5, 4) = 0.4786286705<br />

cis(5, 5) = 0.236926885<br />

ci = cis(i, j)<br />

End Function<br />

Además debemos incluir un mensaje en el caso de que se seleccione un grado menor que 2 mayor que 5.<br />

El código del botón es<br />

Private Sub CommandButton1_Click()<br />

Dim suma As Double<br />

Dim n As Integer<br />

Dim g As String<br />

Dim OK As Boolean<br />

Dim Fun As New clsMathParser ’ así se llama el módulo de clase aquí<br />

g = Cells(2, 2)<br />

n = Cells(3, 5)<br />

Cells(5, 6) = "" ’limpiar cálculos anteriores<br />

If n < 2 Or n > 5 Then<br />

MsgBox ("Solo tenemos datos <strong>para</strong> n entre 2 y 5")<br />

Exit Sub<br />

End If<br />

OK = Fun.StoreExpression(g) ’formula actual es ’formula’<br />

If Not OK Then GoTo Error_Handler<br />

suma = 0<br />

For i = 1 To n<br />

suma = suma + ci(n, i) * Fun.Eval1(xi(n, i))<br />

Next i<br />

Cells(5, 6) = suma<br />

’---------------------------------------------------------------<br />

If Err Then GoTo Error_Handler<br />

Error_Handler: Cells(1,1)=Fun.ErrorDescription ’Mensaje de error<br />

’---------------------------------------------------------------<br />

End Sub

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!