No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

No se trata de lo que quieres comprar, sino de quién quieres ser. Aprovecha el precio especial.

Antes: $249

Currency
$209

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Suscríbete

Termina en:

14 Días
6 Hrs
35 Min
21 Seg

If - Then

13/17
Recursos

Aportes 127

Preguntas 7

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

Btw, Si se sienten más cómodos usando las funciones que ya conocían en excel, pueden usarlo mediante la siguiente sintaxis:

Application.WorksheetFunction.<excel_function>

Ejemplo para obtener un valor entre 1 y 6

.
Consideraciones:

  1. Si usan las fórmulas de excel, estas deben estar escritas en su forma oficial en inglés, los argumentos que usa son los mismos
  2. VBA a diferencia de otros lenguajes, no es case sensitive, la declaración de la variable marca la pauta. Ejm. Si declaras Dim aLeAtOrIo as integer y en otra línea usas aleatorio = Application.Worksheet…, verás que al cambiar de línea automáticamente “aleatorio” se cambia por “aLeAtOrIo”
  3. Una práctica común en la mayoría de lenguajes para debuggear es el print statement (o las impresiones intermedias). En el caso de VBA puedes usar Debug.Print <variable> para ver que está ejecutando e ir revisando cada vez que necesitas hacerlo.
Public Function cartas() As String

	Dim NumCar As Integer
	Dim ColCar As Integer
	Dim TipCar As Integer
	Dim RNumCar As String
	Dim RColCar As String
	Dim RTipCar As String
	Dim a

	NumCar = Int((Rnd * 13) + 1)
	ColCar = Int((Rnd * 2) + 1)
	TipCar = Int((Rnd * 4) + 1)

	If NumCar = 1 Then
    	RNumCar = "As"
    ElseIf NumCar = 2 Then
        RNumCar = "2"
    ElseIf NumCar = 3 Then
        RNumCar = "3"
    ElseIf NumCar = 4 Then
        RNumCar = "4"
    ElseIf NumCar = 5 Then
        RNumCar = "5"
    ElseIf NumCar = 6 Then
        RNumCar = "6"
    ElseIf NumCar = 7 Then
        RNumCar = "7"
    ElseIf NumCar = 8 Then
        RNumCar = "8"
    ElseIf NumCar = 9 Then
        RNumCar = "9"
    ElseIf NumCar = 10 Then
        RNumCar = "10"
    ElseIf NumCar = 11 Then
        RNumCar = "Jota"
    ElseIf NumCar = 12 Then
        RNumCar = "Reina"
    ElseIf NumCar = 13 Then
        RNumCar = "Rey"
    End If

    If ColCar = 1 Then
        RColCar = "Negro"
    ElseIf ColCar = 2 Then
        RColCar = "Rojo"
    End If

    If TipCar = 1 Then
        RTipCar = "Tréboles"
    ElseIf TipCar = 2 Then
        RTipCar = "Diamantes"
    ElseIf TipCar = 3 Then
        RTipCar = "Corazones"
    ElseIf TipCar = 4 Then
        RTipCar = "Picas"
    End If
    cartas = RNumCar & " de " & RTipCar & " " & RColCar

    a = MsgBox(cartas)

	End Function

Así programé una función que te da una de las 52 cartas de la baraja clásica de poker al azar 😃

Mas practico

Public Function dado() As String

    Dim VD As Integer
    Dim a
    VD = Int((Rnd * 6) + 1)
    If (VD Mod 2) = 0 Then
        dado = "Par"
    Else: dado = "Impar"
    End If
    
    a = MsgBox(VD)
    
End Function

Les comparto el reto de generar una carta de baraja inglesa de manera aleatoria.

' Carta Aleatoria
Public Function cartas() As String

    Dim C As Integer
    Dim CU As Integer
    Dim CUs As String
    
    Dim pinta As String
    Dim cartastring As String
    
    C = Int((52 * Rnd) + 1)
    
    ' Definir rangos que le pertenecen a cada pinta
    If C >= 1 And C <= 13 Then
        pinta = "Picas"
        ElseIf C >= 14 And C <= 26 Then
        pinta = "Corazones"
        ElseIf C >= 27 And C <= 39 Then
        pinta = "Treboles"
        Else: pinta = "Diamantes"
    End If
    
    ' Convertir los corazones, treboles y diamantes a números entre el 1 y el 13
    If C >= 1 And C <= 13 Then
        CU = C - 0
        ElseIf C >= 14 And C <= 26 Then
            CU = C - 13
        ElseIf C >= 27 And C <= 39 Then
            CU = C - 26
        ElseIf C >= 40 And C <= 52 Then
            CU = C - 39
        End If
        
        CUs = CU
        
    ' Nombrar las cartas 1, 11, 12 y 13 como A, J, Q, K respectivamente
    If CUs = "1" Then
        cartastring = "A"
        ElseIf CUs = "11" Then
            cartastring = "J"
        ElseIf CUs = "12" Then
            cartastring = "Q"
        ElseIf CUs = "13" Then
            cartastring = "K"
        Else: cartastring = CUs
    End If
    
cartas = "La carta generada aleatoriamente es " + cartastring + " de " + pinta
            
End Function

Casi que no lo logro

Aplicando if , funcion en VBA y en la hoja Aleatorio entre y una tabla al cual le di el nombre de figuras del WIN+[.]
aplique tambien formato condicional para el color de la celda en la tabla. Se puede hacer diversidad depende del Usuario.
Sintaxis y Aplicacion de la Funcion en Hoja:

Esto lo generó una IA, comparto el resultado:

Sub ChooseCard()
’ Declare variables
Dim suits(1 To 4) As String
Dim numbers(1 To 13) As String
Dim suit As String
Dim number As String
Dim card As String

’ Initialize arrays of suits and numbers
suits(1) = "Spades"
suits(2) = "Hearts"
suits(3) = "Clubs"
suits(4) = “Diamonds”

numbers(1) = "Ace"
numbers(2) = "Two"
numbers(3) = "Three"
numbers(4) = "Four"
numbers(5) = "Five"
numbers(6) = "Six"
numbers(7) = "Seven"
numbers(8) = "Eight"
numbers(9) = "Nine"
numbers(10) = "Ten"
numbers(11) = "Jack"
numbers(12) = "Queen"
numbers(13) = “King”

’ Choose a random suit and number
suit = suits(Int((4 - 1 + 1) * Rnd + 1))
number = numbers(Int((13 - 1 + 1) * Rnd + 1))

’ Construct the card name
card = number & " of " & suit

’ Determine whether the card is red or black
If suit = “Hearts” Or suit = “Diamonds” Then
MsgBox card & " is a red card."
Else
MsgBox card & " is a black card."
End If
End Sub

Hola, adjunto código del reto.

Resultados

Con Debug.Print y la variable podemos imprimir el valor de la variable

Public Function dado() As String
    Dim VD As Integer
    
    VD = Int((Rnd * 6) + 1)
    
    If VD Mod 2 = 0 Then
        dado = "par"
    Else
    dado = "Impar"
    End If
    Debug.Print VD
End Function

Comparto funcion para mostrar una carta aleatoriamente.

Una forma de crear esta funcion es utilizando el operador de modulo Mod en VBA. Lo que hace este operador es obtener el resto de la division entre dos numeros. En este caso si le pasamos un numero como parametro a la funcion dado(), lo dividimos entre dos y el resultado nos da cero, eso signidifica que el numero es par, sino, nos regresara impar. De esta forma si utilizamos este mismo algoritmo para listas de numeros mas grandes no tendremos que escribir cada caso que tengamos.

Public Function dado(number As Integer) As String
    Dim a
    If number Mod 2 = 0 Then
            dado = "Par"
        ElseIf number Mod 2 = 1 Then
            dado = "Impar"
    End If
    a = MsgBox(number)
End Function••••ˇˇˇˇ

Lo terminé haciendo con select case para no usar tanto código, pero igual utilicé una parte de con If - Then. Usé el naipe español.

Public Function baraja() As String

    Dim palo As Integer
    Dim num_carta As Integer
    Dim carta As String
    Dim palo_carta As String
    
    palo = Int((Rnd * 4) + 1)
    num_carta = Int((Rnd * 10) + 1)

    If palo = 1 Then
            palo_carta = "Oros"
        ElseIf palo = 2 Then
            palo_carta = "Copas"
        ElseIf palo = 3 Then
            palo_carta = "Espadas"
        Else: palo_carta = "Bastos"
    End If
    
    Select Case palo_carta
        Case "Oros"
            Select Case num_carta
                Case 1, 2, 3, 4, 5, 6, 7
                    carta = num_carta & " de " & palo_carta
                Case 8, 9, 10
                    num_carta = num_carta + 2
                    carta = num_carta & " de " & palo_carta
            End Select
        Case "Copas"
            Select Case num_carta
                Case 1, 2, 3, 4, 5, 6, 7
                    carta = num_carta & " de " & palo_carta
                Case 8, 9, 10
                    num_carta = num_carta + 2
                    carta = num_carta & " de " & palo_carta
            End Select
        Case "Copas"
            Select Case num_carta
                Case 1, 2, 3, 4, 5, 6, 7
                    carta = num_carta & " de " & palo_carta
                Case 8, 9, 10
                    num_carta = num_carta + 2
                    carta = num_carta & " de " & palo_carta
            End Select
        Case Else
            Select Case num_carta
                Case 1, 2, 3, 4, 5, 6, 7
                    carta = num_carta & " de " & palo_carta
                Case 8, 9, 10
                    num_carta = num_carta + 2
                    carta = num_carta & " de " & palo_carta
            End Select
    End Select
                    
    a = MsgBox(carta)
End Function

Maravillosas estas clases, me traen lindos recuerdos !
Gracias

Comparto solución para la selección de cartas aleatorias ♥♣♦♠

Mi solucion respecto a las carta seria de esta manera

Eh realizado uno con subrutina donde lo que hará que si la celda A1 es mayor a cero se pintara de verde si es menor a cero se pintara de rojo

What do you think? I Could do it with less text ```js Public Function dice() As String Dim VD As Integer Dim dif As Double Dim a As String VD = Int(Rnd * 6 + 1) dif = (VD / 2) - Int(VD / 2) If dif <= 0 Then dice = "Pair" Else: dice = "odd" End If a = MsgBox(VD) End Function ```Public Function dice() As String Dim VD As Integer Dim dif As Double Dim a As String VD = Int(Rnd \* 6 + 1) dif = (VD / 2) - Int(VD / 2) If dif <= 0 Then dice = "Pair" Else: dice = "odd" End If a = MsgBox(VD) End Function
```txt Public Function cartas() As String Dim color As String Dim numCarta As Integer Dim figuraCarta As String Dim pinta As String Dim mensaje As String color = InputBox("Elige un color de cartas: ") Select Case color Case Is = "rojo" color = "rojo" Case Is = "azul" color = "azul" Case Else color = "Color no válido, por favor elige entre los colores azul y rojo" End Select numCarta = Int(Rnd * 13) + 1 If numCarta = 1 Then figuraCarta = "As" ElseIf numCarta = 11 Then figuraCarta = "J" ElseIf numCarta = 12 Then figuraCarta = "Q" ElseIf numCarta = 13 Or 15 Then figuraCarta = "K" Else: figuraCarta = 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8 Or 9 Or 10 End If pintaCartas = Int(Rnd * 4) + 1 If pintaCartas = 1 Then pinta = "Diamantes" ElseIf pintaCartas = 2 Then pinta = "Picas" ElseIf pintaCartas = 3 Then pinta = "Corazones" Else: pinta = "Trébol" End If mensaje = MsgBox("Elegiste la carta de color " & color & ", te salió " & figuraCarta & " de " & pinta) End Function ```
```js Public Function carta() As String Dim CT As Integer Dim Color As Integer Dim Simbolo As Integer Dim a CT = Int((Rnd * 6) + 1) Color = Int((Rnd * 2) + 1) Simbolo = Int((Rnd * 2) + 1) If CT = 1 And Color = 1 And Simbolo = 1 Then carta = "1, rojo, corazon" ElseIf CT = 2 And Color = 1 And Simbolo = 1 Then carta = "2, rojo, corazon" ElseIf CT = 3 And Color = 1 And Simbolo = 1 Then carta = "3, rojo, corazon" ElseIf CT = 4 And Color = 1 And Simbolo = 1 Then carta = "4, rojo, corazon" ElseIf CT = 5 And Color = 1 And Simbolo = 1 Then carta = "5, rojo, corazon" ElseIf CT = 6 And Color = 1 And Simbolo = 1 Then carta = "6, rojo, corazon" ElseIf CT = 7 And Color = 1 And Simbolo = 1 Then carta = "7, rojo, corazon" ElseIf CT = 8 And Color = 1 And Simbolo = 1 Then carta = "8, rojo, corazon" ElseIf CT = 9 And Color = 1 And Simbolo = 1 Then carta = "9, rojo, corazon" ElseIf CT = 10 And Color = 1 And Simbolo = 1 Then carta = "10, rojo, corazon" ElseIf CT = 11 And Color = 1 And Simbolo = 1 Then carta = "J, rojo, corazon" ElseIf CT = 12 And Color = 1 And Simbolo = 1 Then carta = "Q, rojo, corazon" ElseIf CT = 13 And Color = 1 And Simbolo = 1 Then carta = "K, rojo, corazon" '---------------' ElseIf CT = 1 And Color = 1 And Simbolo = 2 Then carta = "1, rojo, rombo" ElseIf CT = 2 And Color = 1 And Simbolo = 2 Then carta = "2, rojo, rombo" ElseIf CT = 3 And Color = 1 And Simbolo = 2 Then carta = "3, rojo, rombo" ElseIf CT = 4 And Color = 1 And Simbolo = 2 Then carta = "4, rojo, rombo" ElseIf CT = 5 And Color = 1 And Simbolo = 2 Then carta = "5, rojo, rombo" ElseIf CT = 6 And Color = 1 And Simbolo = 2 Then carta = "6, rojo, rombo" ElseIf CT = 7 And Color = 1 And Simbolo = 2 Then carta = "7, rojo, rombo" ElseIf CT = 8 And Color = 1 And Simbolo = 2 Then carta = "8, rojo, rombo" ElseIf CT = 9 And Color = 1 And Simbolo = 2 Then carta = "9, rojo, rombo" ElseIf CT = 10 And Color = 1 And Simbolo = 2 Then carta = "10, rojo, rombo" ElseIf CT = 11 And Color = 1 And Simbolo = 2 Then carta = "J, rojo, rombo" ElseIf CT = 12 And Color = 1 And Simbolo = 2 Then carta = "Q, rojo, rombo" ElseIf CT = 13 And Color = 1 And Simbolo = 2 Then carta = "K, rojo, rombo" '---------------' ElseIf CT = 1 And Color = 2 And Simbolo = 1 Then carta = "1, negro, pika" ElseIf CT = 2 And Color = 2 And Simbolo = 1 Then carta = "2, negro, pika" ElseIf CT = 3 And Color = 2 And Simbolo = 1 Then carta = "3, negro, pika" ElseIf CT = 4 And Color = 2 And Simbolo = 1 Then carta = "4, negro, pika" ElseIf CT = 5 And Color = 2 And Simbolo = 1 Then carta = "5, negro, pika" ElseIf CT = 6 And Color = 2 And Simbolo = 1 Then carta = "6, negro, pika" ElseIf CT = 7 And Color = 2 And Simbolo = 1 Then carta = "7, negro, pika" ElseIf CT = 8 And Color = 2 And Simbolo = 1 Then carta = "8, negro, pika" ElseIf CT = 9 And Color = 2 And Simbolo = 1 Then carta = "9, negro, pika" ElseIf CT = 10 And Color = 2 And Simbolo = 1 Then carta = "10, negro, pika" ElseIf CT = 11 And Color = 2 And Simbolo = 1 Then carta = "J, negro, pika" ElseIf CT = 12 And Color = 2 And Simbolo = 1 Then carta = "Q, negro, pika" ElseIf CT = 13 And Color = 2 And Simbolo = 1 Then carta = "K, negro, pika" '---------------' ElseIf CT = 1 And Color = 2 And Simbolo = 2 Then carta = "1, negro, trebol" ElseIf CT = 2 And Color = 2 And Simbolo = 2 Then carta = "2, negro, trebol" ElseIf CT = 3 And Color = 2 And Simbolo = 2 Then carta = "3, negro, trebol" ElseIf CT = 4 And Color = 2 And Simbolo = 2 Then carta = "4, negro, trebol" ElseIf CT = 5 And Color = 2 And Simbolo = 2 Then carta = "5, negro, trebol" ElseIf CT = 6 And Color = 2 And Simbolo = 2 Then carta = "6, negro, trebol" ElseIf CT = 7 And Color = 2 And Simbolo = 2 Then carta = "7, negro, trebol" ElseIf CT = 8 And Color = 2 And Simbolo = 2 Then carta = "8, negro, trebol" ElseIf CT = 9 And Color = 2 And Simbolo = 2 Then carta = "9, negro, trebol" ElseIf CT = 10 And Color = 2 And Simbolo = 2 Then carta = "10, negro, trebol" ElseIf CT = 11 And Color = 2 And Simbolo = 2 Then carta = "J, negro, trebol" ElseIf CT = 12 And Color = 2 And Simbolo = 2 Then carta = "Q, negro, trebol" ElseIf CT = 13 And Color = 2 And Simbolo = 2 Then carta = "K, negro, trebol" End If a = MsgBox(CT) + MsgBox(Color) + MsgBox(Simbolo) End Function ```Public Function carta() As String Dim CT As Integer Dim Color As Integer Dim Simbolo As Integer Dim a CT = Int((Rnd \* 6) + 1) Color = Int((Rnd \* 2) + 1) Simbolo = Int((Rnd \* 2) + 1) If CT = 1 And Color = 1 And Simbolo = 1 Then carta = "1, rojo, corazon" ElseIf CT = 2 And Color = 1 And Simbolo = 1 Then carta = "2, rojo, corazon" ElseIf CT = 3 And Color = 1 And Simbolo = 1 Then carta = "3, rojo, corazon" ElseIf CT = 4 And Color = 1 And Simbolo = 1 Then carta = "4, rojo, corazon" ElseIf CT = 5 And Color = 1 And Simbolo = 1 Then carta = "5, rojo, corazon" ElseIf CT = 6 And Color = 1 And Simbolo = 1 Then carta = "6, rojo, corazon" ElseIf CT = 7 And Color = 1 And Simbolo = 1 Then carta = "7, rojo, corazon" ElseIf CT = 8 And Color = 1 And Simbolo = 1 Then carta = "8, rojo, corazon" ElseIf CT = 9 And Color = 1 And Simbolo = 1 Then carta = "9, rojo, corazon" ElseIf CT = 10 And Color = 1 And Simbolo = 1 Then carta = "10, rojo, corazon" ElseIf CT = 11 And Color = 1 And Simbolo = 1 Then carta = "J, rojo, corazon" ElseIf CT = 12 And Color = 1 And Simbolo = 1 Then carta = "Q, rojo, corazon" ElseIf CT = 13 And Color = 1 And Simbolo = 1 Then carta = "K, rojo, corazon" '---------------' ElseIf CT = 1 And Color = 1 And Simbolo = 2 Then carta = "1, rojo, rombo" ElseIf CT = 2 And Color = 1 And Simbolo = 2 Then carta = "2, rojo, rombo" ElseIf CT = 3 And Color = 1 And Simbolo = 2 Then carta = "3, rojo, rombo" ElseIf CT = 4 And Color = 1 And Simbolo = 2 Then carta = "4, rojo, rombo" ElseIf CT = 5 And Color = 1 And Simbolo = 2 Then carta = "5, rojo, rombo" ElseIf CT = 6 And Color = 1 And Simbolo = 2 Then carta = "6, rojo, rombo" ElseIf CT = 7 And Color = 1 And Simbolo = 2 Then carta = "7, rojo, rombo" ElseIf CT = 8 And Color = 1 And Simbolo = 2 Then carta = "8, rojo, rombo" ElseIf CT = 9 And Color = 1 And Simbolo = 2 Then carta = "9, rojo, rombo" ElseIf CT = 10 And Color = 1 And Simbolo = 2 Then carta = "10, rojo, rombo" ElseIf CT = 11 And Color = 1 And Simbolo = 2 Then carta = "J, rojo, rombo" ElseIf CT = 12 And Color = 1 And Simbolo = 2 Then carta = "Q, rojo, rombo" ElseIf CT = 13 And Color = 1 And Simbolo = 2 Then carta = "K, rojo, rombo" '---------------' ElseIf CT = 1 And Color = 2 And Simbolo = 1 Then carta = "1, negro, pika" ElseIf CT = 2 And Color = 2 And Simbolo = 1 Then carta = "2, negro, pika" ElseIf CT = 3 And Color = 2 And Simbolo = 1 Then carta = "3, negro, pika" ElseIf CT = 4 And Color = 2 And Simbolo = 1 Then carta = "4, negro, pika" ElseIf CT = 5 And Color = 2 And Simbolo = 1 Then carta = "5, negro, pika" ElseIf CT = 6 And Color = 2 And Simbolo = 1 Then carta = "6, negro, pika" ElseIf CT = 7 And Color = 2 And Simbolo = 1 Then carta = "7, negro, pika" ElseIf CT = 8 And Color = 2 And Simbolo = 1 Then carta = "8, negro, pika" ElseIf CT = 9 And Color = 2 And Simbolo = 1 Then carta = "9, negro, pika" ElseIf CT = 10 And Color = 2 And Simbolo = 1 Then carta = "10, negro, pika" ElseIf CT = 11 And Color = 2 And Simbolo = 1 Then carta = "J, negro, pika" ElseIf CT = 12 And Color = 2 And Simbolo = 1 Then carta = "Q, negro, pika" ElseIf CT = 13 And Color = 2 And Simbolo = 1 Then carta = "K, negro, pika" '---------------' ElseIf CT = 1 And Color = 2 And Simbolo = 2 Then carta = "1, negro, trebol" ElseIf CT = 2 And Color = 2 And Simbolo = 2 Then carta = "2, negro, trebol" ElseIf CT = 3 And Color = 2 And Simbolo = 2 Then carta = "3, negro, trebol" ElseIf CT = 4 And Color = 2 And Simbolo = 2 Then carta = "4, negro, trebol" ElseIf CT = 5 And Color = 2 And Simbolo = 2 Then carta = "5, negro, trebol" ElseIf CT = 6 And Color = 2 And Simbolo = 2 Then carta = "6, negro, trebol" ElseIf CT = 7 And Color = 2 And Simbolo = 2 Then carta = "7, negro, trebol" ElseIf CT = 8 And Color = 2 And Simbolo = 2 Then carta = "8, negro, trebol" ElseIf CT = 9 And Color = 2 And Simbolo = 2 Then carta = "9, negro, trebol" ElseIf CT = 10 And Color = 2 And Simbolo = 2 Then carta = "10, negro, trebol" ElseIf CT = 11 And Color = 2 And Simbolo = 2 Then carta = "J, negro, trebol" ElseIf CT = 12 And Color = 2 And Simbolo = 2 Then carta = "Q, negro, trebol" ElseIf CT = 13 And Color = 2 And Simbolo = 2 Then carta = "K, negro, trebol" End If a = MsgBox(CT) + MsgBox(Color) + MsgBox(Simbolo) End Function
Bueno, yo primero entendí que era la baraja española y lo hice de esa forma y después lo hice para la baraja americana. Para baraja americana ```js Public Function cart_poker() As String Dim NCart As Integer Dim MCart As Integer Dim NumC As String Dim MazoC As String Dim ColorC As String Dim a NCart = Int((Rnd * 13) + 1) MCart = Int((Rnd * 4) + 1) If NCart = 1 Then NumC = "As" ElseIf NCart = 11 Then NumC = "Jota" ElseIf NCart = 12 Then NumC = "Reina" ElseIf NCart = 13 Then NumC = "Rey" Else: NumC = NCart End If If MCart = 1 Then MazoC = "Diamantes" ColorC = "Rojo" ElseIf MCart = 2 Then MazoC = "Picas" ColorC = "Negro" ElseIf MCart = 3 Then MazoC = "Corazones" ColorC = "Rojo" Else: MazoC = "Trébol" ColorC = "Negro" End If a = MsgBox("La carta es " & NumC & " de " & MazoC & " de color " & ColorC, , "Cartas de Poker") End Function ```Y para la española este es el código ```js Public Function cartas_espa() As String Dim NumCar As Integer Dim MazoCar As Integer Dim Carta As String Dim Mazo As String Dim a NumCar = Int((Rnd * 10) + 1) If NumCar > 7 Then NumCar = NumCar + 2 End If If NumCar = 1 Then Carta = "As" ElseIf NumCar = 10 Then Carta = "Sota" ElseIf NumCar = 11 Then Carta = "Caballo" ElseIf NumCar = 12 Then Carta = "Rey" Else: Carta = NumCar End If MazoCar = Int((Rnd * 4) + 1) If MazoCar = 1 Then Mazo = "Oros" ElseIf MazoCar = 2 Then Mazo = "Copas" ElseIf MazoCar = 3 Then Mazo = "Espadas" Else: Mazo = "Bastos" End If a = MsgBox("La carta es " & Carta & " de " & Mazo, , "Cartas") End Function ```
![](https://static.platzi.com/media/user_upload/image-2402887b-457f-4bd4-af1e-2bcecd4ee620.jpg)
```js Function Cartas() As String Dim Num As Integer Dim sim As Integer Dim col As Integer Dim qNum As String Dim qsim As String Dim qcol As String Num = Int((Rnd * 13) + 1) sim = Int((Rnd * 4) + 1) col = Int((Rnd * 2) + 1) If Num = 1 Then qNum = "As" ElseIf Num = 11 Then qNum = "Jota" ElseIf Num = 12 Then qNum = "Reina" ElseIf Num = 13 Then qNum = "Rey" Else: qNum = Num End If If sim = 1 Then qsim = "Corazones" ElseIf sim = 2 Then qsim = "Tréboles" ElseIf sim = 3 Then qsim = "Picas" Else: qsim = "Diamantes" End If If col = 1 Then qcol = "Rojas" Else: qcol = "Negras" End If Cartas = MsgBox("Tu carta es el: " & qNum & " " & qcol & " de " & qsim) End Function ```Function Cartas() As String Dim Num As Integer Dim sim As Integer Dim col As Integer Dim qNum As String Dim qsim As String Dim qcol As String Num = Int((Rnd \* 13) + 1) sim = Int((Rnd \* 4) + 1) col = Int((Rnd \* 2) + 1) If Num = 1 Then qNum = "As" ElseIf Num = 11 Then qNum = "Jota" ElseIf Num = 12 Then qNum = "Reina" ElseIf Num = 13 Then qNum = "Rey" Else: qNum = Num End If If sim = 1 Then qsim = "Corazones" ElseIf sim = 2 Then qsim = "Tréboles" ElseIf sim = 3 Then qsim = "Picas" Else: qsim = "Diamantes" End If If col = 1 Then qcol = "Rojas" Else: qcol = "Negras" End If Cartas = MsgBox("Tu carta es el: " & qNum & " " & qcol & " de " & qsim) End Function
Un ejemplo de como quedaría haciendo un deck de cartas, se puede mejorar agregando condiciones para indicar que cuando numero = 1 sea un As, o 11, 12 y 13 que sean J, Q y K respectivamente![](https://static.platzi.com/media/user_upload/image-2184319f-b57a-426b-8687-4582c63b7d27.jpg)
Realice el ejercicio de las cartas, pero utilice la Baraja Española que son menos cartas, pero eso no significa que sea menos complejo. ![](https://static.platzi.com/media/user_upload/If%20Then%201-2bc88999-a00f-4a5f-90a1-bc7c313b79b3.jpg) ![](https://static.platzi.com/media/user_upload/If%20Then%202-5c114e6d-686f-401e-96e4-c23032bb1f18.jpg) ![](https://static.platzi.com/media/user_upload/If%20Then%203-efcbba3a-8f77-4ea2-867a-4c9a5e836cbd.jpg) ![](https://static.platzi.com/media/user_upload/If%20Then%20Resultado-73cf5f58-a60e-484c-9711-42d342111c29.jpg)
Prueba de condicionales y números aleatorios: * Definiendo el color y pinta de una baraja de cartas ![](https://static.platzi.com/media/user_upload/Ejercicio5_If-Then-05862547-7972-4718-9e0e-6ea3fc0651a2.jpg)
Me plantee un caso donde las posibilidades fueran mas extensas que 6 escenarios, con la intención de describir como par/impar un resultado entre muchos. ![](https://static.platzi.com/media/user_upload/Screenshot_1-065542e1-38ff-4ce5-bf6f-43b47f3ec818.jpg)
![](https://static.platzi.com/media/user_upload/IF%20-%20THEN2-38e6bb9e-6985-4265-86b8-ad4bfcaaa318.jpg) ![](https://static.platzi.com/media/user_upload/IF%20-%20THEN-ae16f923-8a7c-4113-a523-b56affddb496.jpg) Listos
![](https://static.platzi.com/media/user_upload/image-1360d171-7722-42b3-ae27-1903055eda23.jpg)![](https://static.platzi.com/media/user_upload/image-edb3f461-3e75-4b53-83be-ffb8e880579f.jpg)
![](https://static.platzi.com/media/user_upload/image-7bba8cf9-5d00-4628-95e4-d9bdb93367ed.jpg) ![](https://static.platzi.com/media/user_upload/image-64e64dc5-bca2-4148-99d3-e8479f4dea31.jpg) Este fue mi resultado de la prueba
Public Function Card() As String Dim Num As Integer Dim Tp As Integer Dim x Num = (Rnd \* 12) + 1 Tp = (Rnd \* 3) + 1 If Tp = 1 Then TCard = "picas" ElseIf Tp = 2 Then TCard = "trebol" ElseIf Tp = 3 Then TCard = "corazones" Else: TCard = "diamantes" End If If Tp = "1" Then ccard = "negro" ElseIf Tp = "2" Then ccard = "negro" ElseIf Tp = "3" Then ccard = "Rojo" Else: Tp = "4" ccard = "Rojo" End If Card = ccard & " " & Num & " de " & TCard x = MsgBox(Card) End Function
Public Function Card() As String Dim Num As Integer Dim Tp As Integer Dim x Num = (Rnd \* 12) + 1 Tp = (Rnd \* 3) + 1 If Tp = 1 Then TCard = "picas" ElseIf Tp = 2 Then TCard = "trebol" ElseIf Tp = 3 Then TCard = "corazones" Else: TCard = "diamantes" End If If Tp = "1" Then colcard = "negro" ElseIf Tp = "2" Then colcard = "negro" ElseIf Tp = "3" Then colcard = "Rojo" Else: Tp = "4" colcard = "Rojo" End If Card = Num & " de " & TCard & " color " & colcard x = MsgBox(Card) End Function
Public Function Card() As String Dim Num As Integer Dim Tp As Integer Dim x Num = (Rnd \* 12) + 1 Tp = (Rnd \* 3) + 1 If Tp = 1 Then TCard = "picas" ElseIf Tp = 2 Then TCard = "trebol" ElseIf Tp = 3 Then TCard = "corazones" Else: TCard = "diamantes" End If If Tp = "1" Then colcard = "negro" ElseIf Tp = "2" Then colcard = "negro" ElseIf Tp = "3" Then colcard = "Rojo" Else: Tp = "4" colcard = "Rojo" End If x = MsgBox(Num & " de " & TCard & " color " & colcard) End Function
Public Function Card() As String Dim Rvalor As Integer Dim x Rvalor = (Rnd \* 23) + 1 If Rvalor <= 12 Then Card = "corazones roja" Else: Card = "picas negras" End If x = MsgBox(Rvalor) End Function
Public Function Card() As String Dim Rvalor As Integer Dim x Rvalor = (Rnd \* 12) + 1 Or (Rnd \* 23) + 1 If Rvalor <= 12 Then Card = "corazones roja" Else: Card = "picas negras" End If x = MsgBox(Rvalor) End Function
Public Function Card() As String Dim Rvalor As Integer Dim x Rvalor = (Rnd \* 2) + 1 If Rvalor = 1 Then Card = "roja" Else: Card = "negras" End If x = MsgBox(Rvalor) End Function
![](https://static.platzi.com/media/user_upload/image-ccc99906-ac89-447a-9166-ccbe695cf78b.jpg)
![](https://static.platzi.com/media/user_upload/image-14323852-ae96-4351-8294-faa6e09b05ac.jpg)
Buenas tardes. Este es mi código para la carta seleccionada de poker:```js Public Function poker() As String Dim a Dim carta As String Dim palo As String Dim palo_num As Integer Dim color As String carta = Int((Rnd * 13) + 1) Select Case carta Case Is = 1 carta = "A" Case Is = 11 carta = "J" Case Is = 12 carta = "Q" Case Is = 13 carta = "K" End Select palo_num = Int((Rnd * 4) + 1) Select Case palo_num Case Is = 1 palo = "Corazones" Case Is = 2 palo = "Picas" Case Is = 3 palo = "Diamantes" Case Is = 4 palo = "Trebol" End Select If palo = "Corazones" Then color = "Rojo" ElseIf palo = "Diamantes" Then color = "Rojo" Else: color = "Negro" End If a = MsgBox("La carta elegida es: " & carta & " " & "de " & palo & " " & color, , "Carta Lanzada") End Function ```Public Function poker() As String Dim a Dim carta As String Dim palo As String Dim palo\_num As Integer Dim color As String carta = Int((Rnd \* 13) + 1) Select Case carta Case Is = 1 carta = "A" Case Is = 11 carta = "J" Case Is = 12 carta = "Q" Case Is = 13 carta = "K" End Select palo\_num = Int((Rnd \* 4) + 1) Select Case palo\_num Case Is = 1 palo = "Corazones" Case Is = 2 palo = "Picas" Case Is = 3 palo = "Diamantes" Case Is = 4 palo = "Trebol" End Select If palo = "Corazones" Then color = "Rojo" ElseIf palo = "Diamantes" Then color = "Rojo" Else: color = "Negro" End If a = MsgBox("La carta elegida es: " & carta & " " & "de " & palo & " " & color, , "Carta Lanzada") End Function
Buenas tardes
Amazing!![](https://static.platzi.com/media/user_upload/image-3a273200-a182-4606-9ed3-0f3349094568.jpg)![](https://static.platzi.com/media/user_upload/image-8e18f1c8-d86d-41dd-9636-0b50d00c209a.jpg)![](https://static.platzi.com/media/user_upload/image-5b8b697e-5931-452f-83be-08fe3436df42.jpg)
En VBA tambien tenemos el operador módulo lo que permite acortar un poco más el código. ```txt Public Function dado() As String Dim VD As Integer Dim a VD = Application.WorksheetFunction.RandBetween(1, 6) If VD Mod 2 = 0 Then dado = "par" Else: dado = "impar" End If a = MsgBox(VD) End Function ```
Me pregunto si se pueden guardar funciones nativas de excel en variables y luego como se llamaria a esas variables.
Resolviendo el reto: ![](https://static.platzi.com/media/user_upload/image-bfe20a70-5272-47a7-833f-859965335be7.jpg)
Public Function cartas() As String Dim cart As Integer Dim b cart = (Rnd \* 15) + 1 If cart = 3 Then cartas = "multiplo de 3" ElseIf cart = 6 Then cartas = "multiplo de 3" ElseIf cart = 9 Then cartas = "multiplo de 3" ElseIf cart = 12 Then cartas = "multiplo de 3" ElseIf cart = 15 Then cartas = "multiplo de 3" Else: cartas = "no es multiplo" End If b = MsgBox(cart) End Function
Public Function dado() As String Dim vd As Integer Dim a vd = (Rnd \* 6) + 1 If vd = 2 Then dado = "par" ElseIf vd = 4 Then dado = "par" ElseIf vd = 6 Then dado = "par" Else: dado = "impar2" End If a = MsgBox(vd) End Function Public Function cartas() As String Dim cart As Integer Dim b cart = (Rnd \* 15) + 1 If cart = 3 Then cartas = "multiplo de 3" ElseIf cart = 6 Then cartas = "multiplo de 3" ElseIf cart = 9 Then cartas = "multiplo de 3" ElseIf cart = 12 Then cartas = "multiplo de 3" ElseIf cart = 15 Then cartas = "multiplo de 3" Else: cartas = "no es multiplo" End If b = MsgBox(cart) End Function
Una forma más simple de hacerlo es utilizando la función Módulo.```js Public Function dado() As String Dim VD As Integer Dim a VD = Int(Rnd * 6 + 1) If VD Mod 2 = 0 Then dado = "Par" Else: dado = "Impar" End If a = MsgBox(VD) End Function ```
Una forma más simple de hacerlo es utilizando la función Módulo. ```js Public Function dado() As String Dim VD As Integer Dim a VD = Int(Rnd * 6 + 1) If VD Mod 2 = 0 Then dado = "Par" Else: dado = "Impar" End If a = MsgBox(VD) End Function ```Public Function dado() As String Dim VD As Integer Dim a VD = Int(Rnd \* 6 + 1) If VD Mod 2 = 0 Then dado = "Par" Else: dado = "Impar" End If a = MsgBox(VD) End Function
`Public Function dado() As String` ` Dim VD As Integer` ` VD = Int(Rnd * 6 + 1)` ` Dim residuo As Integer` ` ` ` residuo = VD Mod 2` ` ` ` If residuo = 0 Then` ` dado = VD & " Es par"` ` Else` ` dado = VD & " Es impar"` ` End If` ` ` `End Function`
Se hubiese podido optimizar usando Select Case, creería.
```txt Public Function dado() As String Dim a Dim VD As Integer VD = Int((Rnd * 6) + 1) a = VD Mod 2 If a = 0 Then dado = "Par," & VD Else: dado = "Impar," & VD End If End Function ```Public Function dado() As String Dim a Dim VD As Integer VD = Int((Rnd \* 6) + 1) a = VD Mod 2 If a = 0 Then dado = "Par," & VD Else: dado = "Impar," & VD End If End Function
```txt Sub getCard() 'Definición de variables Dim i As Integer Dim j As Integer Dim varColors As Variant Dim varSuit As Variant Dim varCards As Variant 'Generación de arrays varSuit = Array(Cells(3, 1).Value, Cells(4, 1).Value, Cells(5, 1).Value, Cells(6, 1).Value) varCards = Array("A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K") varColors = Array(1, 10, 3, 5) i = Int(Rnd() * 4) j = Int(Rnd() * 13) Cells(1, 1).Value = varCards(j) & varSuit(i) Cells(1, 1).Font.ColorIndex = varColors(i) End Sub ```
Estaba haciendo el reto de las cartas con condicionales **in then**, sin embargo se me complicó y lo hice usando aleatorios combinado con arrays. En la celda A1 mediante un boton, se genera una carta aleatoria. `Sub getCard()` ` Dim i As Integer` ` Dim j As Integer` ` Dim card As String` ` Dim varColors As Variant` ` Dim varSuit As Variant` ` Dim varCards As Variant` ` ` ` varSuit = Array(Cells(3, 1).Value, Cells(4, 1).Value, Cells(5, 1).Value, Cells(6, 1).Value)` ` varCards = Array("A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K")` ` varColors = Array(1, 10, 3, 5)` ` ` ` i = Int(Rnd() * 4)` ` j = Int(Rnd() * 13)` ` Cells(1, 1).Value = varCards(j) & varSuit(i)` ` ` ` Cells(1, 1).Font.ColorIndex = varColors(i)` ` Cells(1, 1).Font.TintAndShade = 0` `End Sub` Usando condicionales también quedó igual, pero al final fui simplificando... ![](file:///C:/Users/rminchola/Pictures/Screenshots/cards1.png)![](https://static.platzi.com/media/user_upload/cards1-ed8012d7-dfc7-48f7-86ed-eacaaf0214ae.jpg) ![](file:///C:/Users/rminchola/Pictures/Screenshots/cards2.png)![](https://static.platzi.com/media/user_upload/cards2-f860515e-f87c-4cae-bc19-b59a3d58b943.jpg)
![](https://static.platzi.com/media/user_upload/image-529aaf10-b00d-4ef5-be50-d05e5f4dd5b9.jpg)

Mi ejercicio de IF THEM

Les comparto mi ejemplo

Public Function Cartas() As String

    Dim valor_palo As Integer
    Dim valor_naipe As Integer
    Dim palo As String
    Dim msg
    
    valor_palo = Int((Rnd * 4) + 1)
    
    If valor_palo = 1 Then
        palo = "Corazones"
        
        ElseIf valor_palo = 2 Then
            palo = "Diamantes"
        
        ElseIf valor_palo = 3 Then
            palo = "Trebol"
        
        Else
            palo = "Picas"
    
    End If
    
    valor_naipe = Int((Rnd * 13) + 1)
    
    If valor_naipe = 1 Then
        naipe = "As"
        
        ElseIf valor_naipe = 11 Then
            naipe = "J"
        
        ElseIf valor_naipe = 12 Then
            naipe = "Q"
        
        ElseIf valor_naipe = 13 Then
            naipe = "K"
            
        Else
            naipe = valor_naipe
        
    End If
    
    msg = MsgBox(naipe & " de " & palo, , "Baraja Inglesa")
    
End Function

```js Public Function dado1() As String Dim VD As Integer Dim a VD = Int((Rnd * 6) + 1) If (VD Mod 2) = 0 Then dado1 = "Par" Else: dado1 = "Impar" End If a = MsgBox(VD) End Function ```Public Function dado1() As String Dim VD As Integer Dim a VD = Int((Rnd \* 6) + 1) If (VD Mod 2) = 0 Then dado1 = "Par" Else: dado1 = "Impar" End If a = MsgBox(VD) End Function

Public Function cards() As String
Dim card
Dim color
Dim number
Dim cardnum
Dim a
number = Int((Rnd * 13) + 1)
card = Int((Rnd * 4) + 1)
If card = 1 Then
color = "Corazones"
ElseIf card = 2 Then
color = "Pica"
ElseIf card = 3 Then
color = "Diamantes"
Else:
color = "Treboles"
End If

If number >= 1 And number <= 10 Then
    cardnum = number
ElseIf number = 11 Then
    cardnum = "J"
ElseIf nuber = 12 Then
    cardnum = "Q"
Else:
    cardnum = "K"
End If
 a = MsgBox("Tu carta es " & cardnum & " de " & color)

End Function

Muy buena clase…!

Adjunto mi aporte

Ahí va la baraja:

'If - then practice

Public Function Deck() As String

    Dim symbol As Integer
    Dim number As Integer
    Dim symboldraw As String
    Dim numberdraw As String
        
    
    symbol = Int(Rnd() * 4 + 1)
    number = Int(Rnd() * 13 + 1)
    
    If symbol = 1 Then
        symboldraw = "Spades"
    ElseIf symbol = 2 Then
        symboldraw = "Hearts"
    ElseIf symbol = 3 Then
        symboldraw = "Diamonds"
    Else
        symboldraw = "Clubs"
    End If
    
    If number = 11 Then
        numberdraw = "Jack"
    ElseIf number = 12 Then
        numberdraw = "Queen"
    ElseIf number = 13 Then
        numberdraw = "King"
    ElseIf number = 1 Then
        numberdraw = "Ace"
    Else
        numberdraw = number
    End If
        
    Deck = "You draw the card " & numberdraw & " of " & symboldraw & "."
    
End Function

'If - then

Public Function DADO() As String

    Dim tiro As Integer
    tiro = Int((Rnd() * 6) + 1)
        
    If tiro Mod 2 = 0 Then
        DADO = "Es par: " & tiro
    Else
        DADO = "Es impar: " & tiro
    End If
    
End Function

Public Function carta() As String

Dim car As Integer
Dim a
    
car = Int((Rnd * 2))
If car = 0 Then
        cartas = "ROJO"
    Else: cartas = "NEGRO"
End If
        
a = MsgBox(cartas)

End Function

Public Function dado() As String
    Dim VD As Integer

    ' This ensures the random number generator is seeded differently each time
    Randomize

    VD = Int(Rnd * 6) + 1

    ' Instead of multiple ElseIf conditions, use the Mod operator to determine even or odd
    If VD Mod 2 = 0 Then
        dado = "par"
    Else
        dado = "impar"
    End If

    ' Display the result in a message box
    MsgBox "El número generado es: " & VD & " y es " & dado

End Function

El condicional “If-Then” en Excel es una poderosa herramienta para automatizar tareas. Permite establecer condiciones y acciones a seguir en macros, mejorando la eficiencia al procesar datos, resaltar celdas y más, según si se cumple una condición específica.

Utiliza una funcion creada dentro de otra funcion

Tenemos la funcion IP la cual determina si un numero es par o impar

Ahora creamos la funcion dado3 la cual utiliza la funcion IP para determinar si la cara es par o impar

El resultado es el siguiente

Saludos comunidad,

  • Les comparto una alternativa haciendo uso de la función MOD, esto nos permite reducir un poco el código:

  • En este ejemplo, la función MOD calcula el remanente de la división entre el valor de “VD” y 2.

  • Si el remanente es igual a 0, podemos decir que la división entre VD y 2 es exacta por lo que es correcto asumir que el valor es par.

  • En caso de que exista un remanente (a<>0), podemos decir que el valor es impar.

'ejemplo uso condiciones "if" "then"

Private Function dado() As String

    Dim VD As Integer
    VD = Int((Rnd * 6) + 1)
    
    Dim a As Integer
    a = VD Mod 2
    
    Dim b
    
    If a = 0 Then
        b = MsgBox("El valor del dado es: " & VD & " y el valor es par")
    ElseIf a <> 0 Then
        MsgBox ("El valor del dado es: " & VD & " y el valor es impar")
    End If
    
End Function

Dejo mi respuesta al reto

Public Function Poker()
    Dim palo As String
    Dim Numero As Variant
    Numero = Int((13 * Rnd) + 1)
    palo = CStr(Int((4 * Rnd) + 1))

    Select Case palo
        Case "1"
            palo = "picas"
        Case "2"
            palo = "corazones"
        Case "3"
            palo = "diamantes"
        Case "4"
            palo = "tréboles"
    End Select
    
    If Numero = 1 Then
        Numero = "As"
    ElseIf Numero = 11 Then
        Numero = "J"
    ElseIf Numero = 12 Then
        Numero = "Q"
    ElseIf Numero = 13 Then
        Numero = "K"
    End If
    
    MsgBox Numero & " " & palo
 
End Function

Codigo que selecciona la carta aleateoriamente

Public Function carta() As String

Dim VC As Integer
Dim TP As Integer
Dim cartastring As String
Dim Palo As String

Dim a

Dim Resultado As Integer

'definimos el numero de la carta
VC = Int(Rnd * 13 + 1)
    ' Nombrar las cartas 1, 11, 12 y 13 como A, J, Q, K respectivamente
If VC = 1 Then
    cartastring = "A"
    ElseIf VC = "11" Then
        cartastring = "J"
    ElseIf VC = "12" Then
        cartastring = "Q"
    ElseIf VC = "13" Then
        cartastring = "K"
    Else: cartastring = VC
End If

'definimos el palo de la carta
TP = Int(Rnd * 4 + 1)
    Select Case TP
    Case Is = 1
        Palo = "Corazon"
    Case Is = 2
        Palo = "Diamante"
    Case Is = 3
        Palo = "Espada"
    Case Is = 4
        Palo = "Flor"
End Select
  
a = MsgBox("La carta seleccionada es " + cartastring + " de " + Palo)

End Function

‘carta aleatoria’
‘1: as, 2: j, 3: q, 4: K’

Public Function carta() As String
Dim palo As Integer
Dim color As Integer
Dim cartacolor As String
Dim cartapalo As String
Dim a

color = Int(Rnd * 2) + 1
palo = Int(Rnd * 4) + 1

If color = 1 Then
    cartacolor = "rojo"
ElseIf color = 2 Then
    cartacolor = "negra"
End If

If palo = 1 Then
    cartapalo = "As"
ElseIf palo = 2 Then
    cartapalo = "Jota"
ElseIf palo = 3 Then
    cartapalo = "Reina"
Else
    cartapalo = "Rey"
End If

a = MsgBox("La carta seleccionada es: " & cartacolor & " " & cartapalo)

End Function

Genial



Tenía duda con el redondeo de Integer pero en VB tanto 0.1 como 0.9 lo redondea a 0

Hola, hice el ejemplo del dado visto en clase con el operador Residuo, para evitar usar el Elseif cuando el rango de numeros es mas grande. Me dio el mismo resultado y no tengo que escribir todos los casos de Para ya que la compu lo hace solo.

Hola a todos,
Escribí unas líneas que te muestran un palo entre corazones, picas, diamantes y tréboles al azar. Es algo muy básico.

Ejercicio de la clase

Ejercicio dados:

Ejercicio dados

Hola compañeros!

Les comparto 4 maneras de llegar al resultado que nos indica la profesora.

La primera es la mas Rudimentaria. Con los clásicos IF

Public Function dado() As String

    Dim Resultado_de_dado As Integer
    Resultado_de_dado = Int(Rnd * 6) + 1
    
    If Resultado_de_dado = 2 Then
            dado = "El resultado es " & Resultado_de_dado & ". Y es Par."
        ElseIf Resultado_de_dado = 4 Then
            dado = "El resultado es " & Resultado_de_dado & ". Y es Par."
        ElseIf Resultado_de_dado = 6 Then
            dado = "El resultado es " & Resultado_de_dado & ". Y es Par."
        Else:
        
            dado = "El resultado es " & Resultado_de_dado & ". Y es impar."
        End If

            
End Function

`Esta es Otra versión pero ahora aplicando agregando OR

Public Function dadoConOR() As String
    Dim Resultado_de_dado As Integer
    Resultado_de_dado = Int(Rnd * 6) + 1
    If Resultado_de_dado = 2 Or Resultado_de_dado = 4 Or Resultado_de_dado = 6 Then
            dadoConOR = "El resultado es " & Resultado_de_dado & ". Y es Par."
        Else:
            dadoConOR = "El resultado es " & Resultado_de_dado & ". Y es impar."
				End if
				Deebug.if
End Function

'Version con MOD (Para saber si es par o Impar sin tener que calcularlo)

Public Function dadoConMOD() As String

    Dim Resultado_de_dado As Integer
    Resultado_de_dado = Int(Rnd * 6) + 1
    
    If Resultado_de_dado Mod 2 = 0 Then
        dadoConMOD = "El resultado es " & Resultado_de_dado & ". Y es Par."
    Else
        dadoConMOD = "El resultado es " & Resultado_de_dado & ". Y es impar."
    End If
    
    Debug.Print
End Function

Y la ultima con MOD pero tambien usando la funcion ya existente.

Public Function dadoConMODyF() As String

    Dim Resultado_de_dado As Integer
    Resultado_de_dado = Application.WorksheetFunction.RandBetween(1, 6)
    
    If Resultado_de_dado Mod 2 = 0 Then
        dadoConMODyF = "El resultado es " & Resultado_de_dado & ". Y es Par."
    Else
        dadoConMODyF = "El resultado es " & Resultado_de_dado & ". Y es impar."
    End If
    
    Debug.Print
End Function

Hola a todos, les comparto el código que hice para generar de manera aleatoria 2 valores de cartas.

Public Sub cartas()
Dim vpinta1 As String
Dim vpinta2 As String
Dim vnumletra1 As String
Dim vnumletra2 As String
Dim resultado As String
Dim a
vpinta1 = pinta()
vnumletra1 = numletra()
vpinta2 = pinta()
vnumletra2 = numletra()

Do While vnumletra1 = vnumletra2
vnumletra2 = numletra()
Loop

resultado = vnumletra1 & " " & vpinta1 & " " & vnumletra2 & " " & vpinta2

a = MsgBox(resultado)

End Sub

Public Function pinta() As String

Dim randompinta As Integer
Dim randomnumero As Integer

randompinta = Int((Rnd * 4) + 1)

Select Case randompinta
Case 1
pinta = "diamante"
Case 2
pinta = "corazon"
Case 3
pinta = "picas"
Case 4
pinta = "trebol"
End Select

End Function

Public Function numletra() As String

Dim randomnl As Integer

randomnl = Int((Rnd * 13) + 1)

Select Case randomnl
Case 1
numletra = "As"
Case 2
numletra = "2"
Case 3
numletra = "3"
Case 4
numletra = "4"
Case 5
numletra = "5"
Case 6
numletra = "6"
Case 7
numletra = "7"
Case 8
numletra = "8"
Case 9
numletra = "9"
Case 10
numletra = "10"
Case 11
numletra = "J"
Case 12
numletra = "Q"
Case 13
numletra = "K"
Case Else
numletra = " "

End Select

End Function

Buena clase.

Hice el ejercicio, para variar, con la baraja española.

Public Function CARTASUERTE() As String

Dim CARTA As Integer
Dim MAZOID As String
Dim NOMBREMAZO As String

MAZOID = Int((Rnd * 4) + 1)
    
If MAZOID = 1 Then
            NOMBREMAZO = "Oros"
   ElseIf MAZOID = 2 Then
        NOMBREMAZO = "Bastos"
   ElseIf MAZOID = 3 Then
        NOMBREMAZO = "Copas"
   ElseIf MAZOID = 4 Then
        NOMBREMAZO = "Espadas"
 End If
 
CARTA = Int(Rnd * 12) + 1

CARTASUERTE = "Tu carta es " & CARTA & " de " & NOMBREMAZO

End Function

Buena Tarde para todos,
este es mi reto

Ejercicio que elige al azar un número entre el 1 y 13 de los 4 mazos posibles

Public Function cartarandom() As String
    
    Dim carta As Integer
    Dim mazo As String
    Dim mazostring As String
    
    'elegir mazo al azar
    mazo = Int((4 * Rnd) + 1)
    
    If mazo = 1 Then
            mazostring = "Picas"
        ElseIf mazo = 2 Then
            mazostring = "Corazones"
        ElseIf mazo = 3 Then
            mazostring = "Diamantes"
        ElseIf mazo = 4 Then
            mazostring = "Treboles"
    End If
    
    'Número aleatorio entre el 1 y 13
    carta = Int((13 * Rnd) + 1)
        
    cartarandom = "Tu carta aleatoria fue " & carta & " de " & mazostring
    
End Function

Dejo el mio

<Sub Main()
    Dim palo As String

    palo = InputBox("Por favor, escribe el nombre de un palo de la baraja:")
    AsignarColor(palo)
End Sub

Public Function AsignarColor(palo As String) As String
    If palo = "Picas" Or palo = "Tréboles" Then
        AsignarColor = "Negro"
    ElseIf palo = "Corazones" Or palo = "Diamantes" Then
        AsignarColor = "Rojo"
    Else
        AsignarColor = "Palo no válido"
    End If
    
    MsgBox "El color del palo " & palo & " es " & AsignarColor & "."
End Function
> 

Dado con resultado par o impar

'dado para o impar
Public Function dado() As String
    Dim VD As Integer
    
    VD = Int((Rnd * 6) + 1)
    
    If VD Mod 2 = 0 Then
        dado = VD & " par"
    Else
        dado = VD & " impar"
    End If
    
End Function

Por aca mi aporte

Public Function dado() As String
    Dim VD As Integer
    'VD = Int(Rnd * 6 + 1)
    VD = Application.WorksheetFunction.RandBetween(1, 6)
    
    If VD Mod 2 = 0 Then
        dado = "Par"
        Else:
            dado = "Impar"
    End If
    Debug.Print VD
        
End Function
Public Function dado() As String
    Dim VD As Integer
    Dim a
    VD = Int((Rnd * 6) + 1)
    
    If (VD Mod 2) = 0 Then
        dado = "par"
    Else: dado = "impar"
    End If
    
    a = MsgBox(VD & " => " & dado)
        
End Function