ESTRUCTURAS
ESTRUCTURAS
Clear End With End Sub Private Sub CB_DoWhile_Click() Dim RgMatriz As Range, Rg As Range Dim X As Integer With Cells .Clear Set RgMatriz = Range([A1], [F1]) End With On Error Resume Next Do While Rg.Value <> "FIN" X=X+1 Set Rg = RgMatriz.Cells(X) With Rg .Select .Interior.ColorIndex = 4 .Value = X If X = 160 Then .Value = "FIN" End If End With Loop Rg.Interior.ColorIndex = 3 End Sub DO LOOP Option Explicit Private Sub Worksheet_Activate() With Cells .Clear End With End Sub Private Sub CB_DoLoop_Click()
Dim RgMatriz As Range, Rg As Range Dim X As Integer With Cells .Clear Set RgMatriz = Range([A1], [G20]) End With On Error Resume Next Do X=X+1 Set Rg = RgMatriz.Cells(X) With Rg .Select .Interior.ColorIndex = 4 .Value = X If X = 160 Then .Value = "FIN" End If End With Loop While Rg.Value <> "FIN" Rg.Interior.ColorIndex = 3 End Sub DO WHILE LOOP
Private Sub Worksheet_Activate() With Cells .Clear End With End Sub Private Sub CB_DoLoop_Click() Dim Salida As Boolean Dim X As Integer, Y As Integer Cells.Clear 'EJEMPLO1: Application.StatusBar = "Ejemplo de: Do While... (X < 10)" Do While X < 20 X=X+1 If X = 10 Then
Exit Do ' Sale del bucle Do While. End If Application.Wait Now + TimeValue("00:00:01") 'Ralentizo para observar Mejor With Cells(X, 1) .Select .Value = X End With Loop 'EJEMPLO2: Application.StatusBar = "Ejemplo de: Do...Until (X = 17), es decir (Salida = True)" Y=1 Do Application.Wait Now + TimeValue("00:00:01") 'Ralentizo para observar Mejor Y=Y+1 With Cells(9, Y) .Select .Value = X End With If X = 17 Then ' Si la condicin es verdadera. Salida = True ' Establece el valor a True. End If X=X+1 Loop Until Salida = True ' Sale inmediatamente del bucle Do...Loop Until. 'EJEMPLO3: Application.StatusBar = "Ejemplo de: Do...Loop hasta que: (Application.WorksheetFunction.COUNTA(Cells) >= 26)" Do Application.Wait Now + TimeValue("00:00:01") 'Ralentizo para observar Mejor With Cells(X - 8, 2) .Select .Interior.ColorIndex = 3 .Value = X End With If Application.WorksheetFunction.CountA(Cells) >= 26 Then ' Sale de este bucle DO...Loop Exit Do End If X=X+1 Loop Application.StatusBar = ""
[A1].Select End Sub DO EVENTS Option Explicit Private Sub Worksheet_Activate() Sheets("DoEvents").Shapes("CB_DoEvents").Visible = True Cells.Clear End Sub
'Cuando el Boton CB_DoEvents_GotFocus es ejecutada si posicionamos 'nuestro cursor sobre cualquier celda, La macro de evento Worksheet_SelectionChange 'Es accionada y hasta que esta no termina de ejecutarse la macro adjunta al 'Boton DoEvents es decir: "CB_DoEvents_GotFocus", no continua. Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Application.StatusBar = "Espere" Then MsgBox "Espera hasta que la Macro Finalice!" Else MsgBox "Puse el Boton DoEvents, y acontinuacion selecciones Cualquier celda" _ & Chr(10) & Chr(10) & "o bien cualquier otro boton en esta misma Hoja." _ & Chr(10) & Chr(10) & "Observaras: Como la Macro DoEvents Pausa su ejecucin.", _ vbInformation, _ "Lee Con Atencin:" End If End Sub 'Esta es la Macro que se ejecuta cuando presionamos sobre el boton: "DoEvents" 'Que al tener dos DoEvents en su interior, detendra su ejecucion mientras, 'Otras macros tomen accion. Private Sub CB_DoEvents_Click() Dim Columna As Byte Dim Fila As Byte Dim Valor As Byte With Application .StatusBar = "Espere" .ScreenUpdating = True
.EnableEvents = False End With With Sheets("DoEvents") .Shapes("CB_DoEvents").Visible = False .Cells.ClearContents .[A1].Select Application.EnableEvents = True For Columna = 1 To 8 'En este ejemplo se utiliza la instruccin DoEvents 'para ceder el control de la ejecucin al sistema operativo 'una vez cada 12 iteraciones del bucle. DoEvents '''''''''''''''*******************''''''''''''''' For Fila = 1 To 20 Valor = Int(Rnd * 50) 'En este ejemplo se utiliza la instruccin DoEvents 'para ceder el control de la ejecucin al sistema operativo 'una vez cada 30 iteraciones del bucle DoEvents '''''''''''''''*******************''''''''''''''' Application.Wait Time + 1 .Cells(Fila, Columna) = Valor Next Fila Next Columna .Shapes("CB_DoEvents").Visible = True End With With Application .StatusBar = "" End With End Sub 'Del mismo modo cuando El Boton DoEvents es pulsado, cuando el boton Rojo, Verde o Sincolor 'son presionadas estas son las macros que ejecutamos: 'Las cuales toman efecto aun cuando la otra no ha acabado Private Sub CB_Verde_Click() With Range("A1:H20") .Interior.ColorIndex = 4 .Cells(1).Select End With End Sub Private Sub CB_Rojo_Click()
With Range("A1:H20") .Interior.ColorIndex = 3 .Cells(1).Select End With End Sub Private Sub CB_SinColor_Click() With Range("A1:H20") .Interior.ColorIndex = xlNone .Cells(1).Select End With End Sub FOR NEXT Option Explicit Private Sub Worksheet_Activate() With Cells .ClearContents End With End Sub Private Sub CB_ForNext_Click() Cells.ClearContents 'EJEMPLO 1) Dim Palabras, Caracteres, MiCadena For Palabras = 10 To 1 Step -1 ' Establece 10 repeticiones. For Caracteres = 0 To 9 ' Establece 10 repeticiones. MiCadena = MiCadena & Caracteres ' Agrega un nmero a la cadena. Next Caracteres ' Incrementa el contador MiCadena = MiCadena & " " ' Agrega un espacio. Next Palabras [A1] = MiCadena Application.Wait Now + TimeValue("00:00:02") 'EJEMPLO 2) Dim X As Integer For X = 2 To 11 With Cells(X, 1) .Select .Interior.ColorIndex = 3
.Value = "A" & X End With Application.Wait Now + TimeValue("00:00:01") Next X Range("A2:A15").Interior.ColorIndex = xlNone End Sub
Private Sub Worksheet_Activate() With Cells .Clear End With End Sub Private Sub CB_ForEachNext_Click() Dim X As Byte Dim Cell As Range Dim ABC As String ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Range("YANIRA").Clear 'EJEMPLO1: For Each Cell In Range("YANIRA") 'Ralentizamos la ejecucion para ver que sucede tal que: Application.Wait Now + TimeValue("00:00:01") With Cell .Select .Value = X .Interior.ColorIndex = 4 .Font.Bold = False End With X=X+1 Next Cell 'Esperamos 3 segundo para el siguiente Ejemplo
Application.Wait Now + TimeValue("00:00:03") 'EJEMPLO2: For Each Cell In Range("YANIRA") 'Ralentizamos la ejecucion para ver que sucede tal que: Application.Wait Now + TimeValue("00:00:01") With Cell .HorizontalAlignment = xlCenter .Value = Mid(ABC, X, 1) 'una letra del abecedario .Interior.ColorIndex = 5 .Font.ColorIndex = 3 .Font.Bold = True End With X=X-1 Next Cell End Sub
IF .. THEN Option Explicit Private Sub Worksheet_Activate() With Range("$B$13:$B$16,$F$13:$F$16") .ClearContents End With End Sub Private Sub CB_IfThen_Click() Dim Rg1 As Range, Rg2 As Range Dim X As Variant, Y As Variant Set Rg1 = Range("C11") Set Rg2 = [E11] 'Union(Rg1, Rg2).NumberFormat = "General" X = Rg1.Value Y = Rg2.Value '1) Caso VACIO If IsEmpty(X) Then Rg1.Offset(2, -1) = "SI" If IsEmpty(X) = False Then Rg1.Offset(2, -1) = "NO" If IsEmpty(Y) Then
Rg2.Offset(2, 1) = "SI" Else Rg2.Offset(2, 1) = "NO" End If '2) Caso NMERICO: 'Excel considera un valor en blanco como nmerico If IsNumeric(X) = True Then Rg1.Offset(3, -1) = "SI" If IsNumeric(X) = False Then Rg1.Offset(3, -1) = "NO" If IsNumeric(Y) Then Rg2.Offset(3, 1) = "SI" Else Rg2.Offset(3, 1) = "NO" End If '3) Caso TEXTO: Los valore de Texto tb se consideran como de Fecha If Application.WorksheetFunction.IsText(X) = True Then Rg1.Offset(4, -1) = "SI" If Application.WorksheetFunction.IsText(X) = False Then Rg1.Offset(4, -1) = "NO" If Application.WorksheetFunction.IsText(Y) Then Rg2.Offset(4, 1) = "SI" Else Rg2.Offset(4, 1) = "NO" End If '4) Caso DATE: If IsDate(X) = True Then Rg1.Offset(5, -1) = "SI" If IsDate(X) = False Then Rg1.Offset(5, -1) = "NO" If IsDate(Y) Then Rg2.Offset(5, 1) = "SI" Else Rg2.Offset(5, 1) = "NO" End If [A1].Select End Sub
With Cells .ClearContents End With End Sub Private Sub CB_Saludos_Click() Select Case Time Case Is < TimeValue("08:30:00"): MsgBox "Buenos Dias!" Case Is >= TimeValue("20:30:00"): MsgBox "Buenas Noches!" Case Else: MsgBox "Buenas Tardes!" End Select [A1].Select End Sub Private Sub DB_WeekDay_Click() Select Case Weekday(Now, vbMonday) ' o bien: Weekday(Now, 2) Case 1: MsgBox "Hoy es Lunes" Case 2: MsgBox "Hoy es Martes " Case 3: MsgBox "Hoy es Mircoles" Case 4: MsgBox "Hoy es Jueves" Case 5: MsgBox "Hoy es Viernes" Case 6: MsgBox "Hoy es Sbado" Case 7: MsgBox "Hoy es Domingo" End Select [A1].Select Call MacroDiaSemana End Sub 'La Macro que responde al boton "Da de la Semana? 'Puede quedar resumido a la Macro "MacroDiaSemana" 'con lo cual si ejecutamos el Boton "Da de la Semana? 'obtendremos el mismo mensaje 2 veces. Sub MacroDiaSemana() Dim ArrayDiaSemana ArrayDiaSemana = Array("", "Lunes", "Martes", "Mircoles", "Jueves", "Viernes", "Sbado", "Domingo") MsgBox "Hoy es: " & ArrayDiaSemana(Weekday(Now, vbMonday)), vbInformation, "Repeticin" End Sub
'En este caso aadimos un Select Case, dentro de otro Select Case, tal que:
'si el valor de la celda B2, no contiene ningn valor comprendido entre el 1 y el 12 'ejecutamos otra intruccion Select Case, que nos sirve para dar un mensaje de Bien Venida 'en el Titulo del mensaje que lanzamos Private Sub CBNmero_Click() Dim X As Variant Dim Msg As String X = Range("B2").Value Select Case X Case Is > 6: MsgBox "B2 es Nmerico y Superior 6." Case Is = 6: MsgBox "B2 es Nmerico e Igual a 6." Case 1 To 6: MsgBox "B2 es Nmerico e Inferior a 6." Case Else Select Case Time Case Is < TimeValue("08:30:00"): Msg = "Buenos Dias" Case Is >= TimeValue("20:30:00"): Msg = "Buenas Noches" Case Else: Msg = "Buenas Tardes" End Select MsgBox "La Celda B2 Esta Vacia.", _ vbInformation, _ Msg & ", pero..." End Select [A1].Select End Sub WHILE WEND
While Not IsEmpty(ActiveCell) 'Si la celda activa no esta vacia... 'Ralentizamos la ejecucion para ver que sucede tal que: Application.Wait Now + TimeValue("00:00:01") ActiveCell.EntireRow.Interior.ColorIndex = 15 ActiveCell.Offset(2).Select Wend 'De lo contarrio slimos del Looping End Sub
Private Sub Worksheet_Activate() With Cells .Clear End With Range("A1:A20") = "AAAA" End Sub Private Sub CB_WhileWend_Click() Cells.Clear 'Limpiamos la hoja Range("A1:A20") = "AAAA" Application.Wait Now + TimeValue("00:00:02") 'EJEMPLO1: [A1].Select