jueves, 4 de diciembre de 2008

VBA Excel: Brain Training


Sub InventarOperaciones()
    Dim i As Integer
    Dim NumOperaciones As Integer
    Dim operacion As String
    NumOperaciones = 10
    For i = 1 To NumOperaciones
        Cells(i, 1) = Int((Rnd * 10) + 1)
        operacion = Int((Rnd * 3) + 1)
        If operacion = "1" Then operacion = "+"
        If operacion = "2" Then operacion = "-"
        If operacion = "3" Then operacion = "*"
        Cells(i, 2) = operacion
        Cells(i, 3) = Int((Rnd * 10) + 1)
        Cells(i, 4) = ""
        Cells(i, 5) = ""
    Next
    Cells(1, 4).Select
End Sub

Sub ComprobarOperaciones()
    Dim i As Integer
    Dim NumOperaciones As Integer
    Dim operacion As String
    Dim resultado As Integer
    Dim puntos As Integer
    NumOperaciones = 10
    puntos = 0
    For i = 1 To NumOperaciones
        operacion = Cells(i, 2)
        If operacion = "+" Then resultado = Cells(i, 1) + Cells(i, 3)
        If operacion = "-" Then resultado = Cells(i, 1) - Cells(i, 3)
        If operacion = "*" Then resultado = Cells(i, 1) * Cells(i, 3)
        If Cells(i, 4) = resultado Then
            Cells(i, 5) = "OK"
            puntos = puntos + 1
        Else
            Cells(i, 5) = resultado
        End If
        
    Next
    MsgBox "PUNTOS:" & puntos
End Sub

martes, 2 de diciembre de 2008

Access VBA: Realizar una consulta por código


Private Sub tipo_AfterUpdate()
    Dim r As DAO.Recordset
    Dim db As DAO.Database
    Dim SQL As String
    Set db = CurrentDb
    SQL = "select * from servicios where codservicio=" & tipo
    Set r = db.OpenRecordset(SQL)
    descripcion = r(1)
    precio = r(2)
End Sub