Excel - ciclo for

 
Vista:
sin imagen de perfil
Val: 43
Ha disminuido su posición en 8 puestos en Excel (en relación al último mes)
Gráfica de Excel

ciclo for

Publicado por mauricio (19 intervenciones) el 20/07/2020 22:20:28
tengo el siguiente codigo: para la fila 6 y 7 lo que requiero hacer es poder recorrer las filas sin ir copiando el código nuevamente como en el ejemplo:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'FILA 6
 
If Range("e6") = 1 And IsEmpty(Range("f6").Value) = True And IsEmpty(Range("g6").Value) = True And IsEmpty(Range("h6").Value) = True Then
 
Range("F6").Select
ActiveCell.FormulaR1C1 = "3"
Range("G6").Select
ActiveCell.FormulaR1C1 = "2"
Range("H6").Select
ActiveCell.FormulaR1C1 = "1"
 
ElseIf Range("e6") = 2 And Range("f6") = Empty And Range("g6") = Empty And Range("h6") = Empty Then
 
Range("F6").Select
ActiveCell.FormulaR1C1 = "1"
Range("G6").Select
ActiveCell.FormulaR1C1 = "3"
Range("H6").Select
ActiveCell.FormulaR1C1 = "2"
 
ElseIf Range("e6") = 3 And Range("f6") = Empty And Range("g6") = Empty And Range("h6") = Empty Then
 
Range("F6").Select
ActiveCell.FormulaR1C1 = "2"
Range("G6").Select
ActiveCell.FormulaR1C1 = "1"
Range("H6").Select
ActiveCell.FormulaR1C1 = "3"
 
End If
 
'FILA 7
 
If Range("e7") = 1 And IsEmpty(Range("f7").Value) = True And IsEmpty(Range("g7").Value) = True And IsEmpty(Range("h7").Value) = True Then
 
Range("F7").Select
ActiveCell.FormulaR1C1 = "3"
Range("G7").Select
ActiveCell.FormulaR1C1 = "2"
Range("H7").Select
ActiveCell.FormulaR1C1 = "1"
 
ElseIf Range("e7") = 2 And Range("f7") = Empty And Range("g7") = Empty And Range("h7") = Empty Then
 
Range("F7").Select
ActiveCell.FormulaR1C1 = "1"
Range("G7").Select
ActiveCell.FormulaR1C1 = "3"
Range("H7").Select
ActiveCell.FormulaR1C1 = "2"
 
ElseIf Range("e7") = 3 And Range("f7") = Empty And Range("g7") = Empty And Range("h7") = Empty Then
 
Range("F7").Select
ActiveCell.FormulaR1C1 = "2"
Range("G7").Select
ActiveCell.FormulaR1C1 = "1"
Range("H7").Select
ActiveCell.FormulaR1C1 = "3"
 
End If
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Luis
Val: 227
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

ciclo for

Publicado por Luis (22 intervenciones) el 21/07/2020 11:40:49
Hola,

Con ciclo for muy sencillo lo puedes conseguir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Sub Rellenar()
 
'Definimos el inicio
Fila = 6
Columna = 5
 
For x = Fila To 5000
 
If Cells(x, Columna) = Empty Then Exit For  ' Sigue recorriendo hasta que encuentra una celda vacia en la columna
 
If Cells(x, Columna + 1) <> Empty And Cells(x, Columna + 2) <> Empty And Cells(x, Columna + 3) <> Empty Then
'No hace nada si alguna de las 3 columnas tiene datos
ElseIf Cells(x, Columna).Value = 1 Then
 
Cells(x, Columna + 1) = 3
Cells(x, Columna + 2) = 2
Cells(x, Columna + 3) = 1
 
ElseIf Cells(x, Columna).Value = 2 Then
 
Cells(x, Columna + 1) = 1
Cells(x, Columna + 2) = 3
Cells(x, Columna + 3) = 2
 
ElseIf Cells(x, Columna).Value = 3 Then
 
Cells(x, Columna + 1) = 2
Cells(x, Columna + 2) = 1
Cells(x, Columna + 3) = 3
 
End If
 
 
Next x
 
 
End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

ciclo for

Publicado por Antoni Masana (2481 intervenciones) el 21/07/2020 20:24:52
Otra forma de hacerlo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Sub macro()
    Dim Fila as Byte
    ....
    For Fila = 6 to 7
        If Range("e" & Fila) = 1 And IsEmpty(Range("f" & Fila).Value) = True _
                                 And IsEmpty(Range("g" & Fila).Value) = True _
                                 And IsEmpty(Range("h" & Fila).Value) = True Then
 
            Range("F" & Fila).Select
            ActiveCell.FormulaR1C1 = "3"
            Range("G" & Fila).Select
            ActiveCell.FormulaR1C1 = "2"
            Range("H" & Fila).Select
            ActiveCell.FormulaR1C1 = "1"
 
        ElseIf Range("e" & Fila) = 2 And Range("f" & Fila) = Empty _
                                     And Range("g" & Fila) = Empty _
                                     And Range("h" & Fila) = Empty Then
 
            Range("F" & Fila).Select
            ActiveCell.FormulaR1C1 = "1"
            Range("G" & Fila).Select
            ActiveCell.FormulaR1C1 = "3"
            Range("H" & Fila).Select
            ActiveCell.FormulaR1C1 = "2"
 
        ElseIf Range("e" & Fila) = 3 And Range("f" & Fila) = Empty _
                                     And Range("g" & Fila) = Empty _
                                     And Range("h" & Fila) = Empty Then
 
            Range("F" & Fila).Select
            ActiveCell.FormulaR1C1 = "2"
            Range("G" & Fila).Select
            ActiveCell.FormulaR1C1 = "1"
            Range("H" & Fila).Select
            ActiveCell.FormulaR1C1 = "3"
 
        End If
    Next
    ...
End Sub

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar