Manual VBA Excel

Page 21

funciona correctamente pero para estos casos es mejor utilizar la instrucción Select Case, el motivo principal es por legibilidad y elegancia. Select Case tiene la sintaxis siguiente: Select Case Expresión Case valores Instrucciones Case valores Instrucciones … Case valores Instrucciones Case Else Instrucciones en caso que no sean ninguno de los valores anteriores. End Select

Vea el ejemplo anterior solucionado con esta estructura. [ Ejemplo 16 ] Sub Ejemplo_16() Dim Signo As String Dim Valor1 As Integer, Valor2 As Integer, Total As Integer Valor1 = ActiveSheet.Range("A1").Value Valor2 = ActiveSheet.Range("A2").Value Signo = ActiveSheet.Range("A3").Value Select Case signo Case "+" Total = Valor1 + Valor2 Case "-" Total = Valor1 - Valor2 Case "x" Total = Valor1 * Valor2 Case ":" Total = Valor1 / Valor2 Case Else Total = 0 End Select ActiveCell.Range("A3").Value = Total End Sub

Centro de Formación solucion.es

pág. 21


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.