Está en la página 1de 16

Excel Avanzado con VBA 04

Ing. Enrique Alfaro

Ing.

Enrique Alfaro

17/02/2013

FUNCIONES DEFINIDAS POR EL USUARIO (UDF)


No pueden ser grabadas, por lo tanto debemos escribir el cdigo VBA. Ejemplo: Function suma2numeros() N1 = Val(InputBox("1er Nmero:")) N2 = Val(InputBox("2do Nmero:")) suma2numeros = N1 + N2 End Function Sub usarfuncion() Range("A1").Value = suma2numeros End Sub

Ing.

Enrique Alfaro

17/02/2013

FUNCIONES DEFINIDAS POR EL USUARIO (UDF)


Ejemplo con 1 parmetro:
Function centigrados(gradosf) centigrados = (gradosf - 32) / 9 * 5 End Function Sub usar() MsgBox (centigrados(100)) End Sub

Tambien podemos usarla directamente en una celda: =centigrados(referencia)

Ing.

Enrique Alfaro

17/02/2013

FUNCIONES DEFINIDAS POR EL USUARIO (UDF)


Creamos la funcin en editor de VBA escribiendo: Function areacirculo(radio) areacirculo = WorksheetFunction.Pi * _ WorksheetFunction.Power(radio, 2) End Function Function volumenesfera(radio) volumenesfera = 4 / 3 * WorksheetFunction.Pi * _ WorksheetFunction.Power(radio, 3) End Function

Ing.

Enrique Alfaro

17/02/2013

SALTAR EL ERROR

Ing.

Enrique Alfaro

17/02/2013

Un ejemplo de Solucin
Sub convertir() cuotasoles = Range("c3").Value cotizac = Range("d5").Value participantes = Range("c8").Value On Error GoTo error cuotadolares = cuotasoles / cotizac recaudacion = participantes * cuotadolares Range("d3").Value = cuotadolares Range("d3").Style = "comma" Range("d8").Value = recaudacion Range("d8").Style = "comma" Exit Sub error: MsgBox ("Divisin por Cero falta cotizacin") End Sub

Ing.

Enrique Alfaro

17/02/2013

Funcin usada como librera


Usamos las funciones directamente en una celda y con su argumento respectivo en otra celda.

Ing.

Enrique Alfaro

17/02/2013

EJEMPLO DE UDF con 2 parmetros

Ing.

Enrique Alfaro

17/02/2013

EJEMPLO DE UDF con 2 parmetros


Function consultaproducto(codigo, col) Set tabla = Sheets(1).Range("B3:D8") consultaproducto = WorksheetFunction.VLookup(codigo, tabla, col, False) End Function Sub consultar() On Error GoTo codigoerrado cod = Val(InputBox("Ingrese codigo producto:", "CONSULTANDO")) Range("D10").Value = consultaproducto(cod, 2) Range("D11").Value = consultaproducto(cod, 3) Range("D12").Value = cod Exit Sub codigoerrado: Range("D12").ClearContents MsgBox ("no es un codigo valido") End Sub

Ing.

Enrique Alfaro

17/02/2013

MANIPULACION DE OBJETOS Y COLECCIONES

OBJETOS Y COLECCIONES
VBA ofrece dos importantes dos importantes estructuras que pueden simplificar el trabajo con objetos y colecciones: 1. Estructuras With ...End With 2. Estructuras For Each...next

ESTRUCTURAS WITH END WITH


Sirve para ejecutar una serie de acciones sobre un mismo Objeto, sin tener que repetir toda su jerarqua
Ej.: Propiedades del objeto Range
Sub Escribe1() ActiveSheet.Range("C7").Value = "Cta. Resultados" ActiveSheet.Range("C7").Font.Bold = True ActiveSheet.Range("C7").Font.Color = RGB(0, 255, 0) End Sub
Sub Escribe2() With ActiveSheet.Range("C7") .Value = "Cta. Resultados" .Font.Bold = True .Font.Color = RGB(0, 255, 0) End With End Sub

Ing.

Enrique Alfaro

17/02/2013

ESTRUCTURAS WITH END WITH


Sub CambiarFuente() With Selection.Font .Name = Times New Roman .FontStyle = Bold Italic .Size = 12 .Underline = xlSingle .ColorIndex = 5 End With End Sub

Ing.

Enrique Alfaro

17/02/2013

ESTRUCTURAS FOR EACH NEXT


No es necesario saber la cantidad de elementos que existen en una coleccin para usar la estructura For Each...Next. Sub ContarHojas() Dim Item As Worksheet declarar el objeto For Each Item In ActiveWorkbook.Sheets MsgBox Item.Name Next Item End Sub

Ing.

Enrique Alfaro

17/02/2013

ESTRUCTURAS FOR EACH NEXT


Sub VentanasAbiertas() Suma = 0 For Each Item In Windows Suma = Suma + 1 Next Item MsgBox Total de ventanas abiertas, & Suma End Sub

Ing.

Enrique Alfaro

17/02/2013

ESTRUCTURAS FOR EACH NEXT


Sub nombresderango() For Each item In Names MsgBox (item.Name) Next item End Sub Si seleccionamos un rango de letras minsculas: Sub ConvertirMayus() For Each celda In Selection celda.Value = UCASE(celda.Value) Next celda End Sub

También podría gustarte