¿Se puede utilizar 2 contadores en un mismo "For"?

Me gustaría saber si hay alguna forma de utilizar un mismo "For" con dos contadores.

Parte del código lo pongo, ya que estoy insertando imágenes con nomenclatura numérica (1,2,3,4,5) y las estoy insertando cada 4 Celdas, empezando con la celda "B6", después la siguiente en "B10", después "B14", "B18"... Etc

Parte del código que es para la imagen es este:

Path = Sheets("Main").Range("C11")

For i = 0 To 6

'n = 3 + i*2

        Set rngCell = Worksheets("Other Printouts").Range("B" & n * 2)

        Li = Path & i & ".jpg"

        Set img = Worksheets("Sheet1").Pictures.Insert(Li)

        With img

            .Left = rngCell.Left

            .Top = rngCell.Top

            .Width = rngCell.Width

            .Height = rngCell.Height

            .Placement = 1

            .PrintObject = True

        End With

Next i

-----------------------------------

Entonces en lugar de utilizar una variable ("n") que depende del contador "i", no se si haya algo así:

For i = 0 to 6 AND n = 6 Step 4

Xxxxxxxxx

Xxxxxxx

Next i,n

------------------------------------

1 respuesta

Respuesta
1

For i = 0 to 6 AND n = 6 Step 4

Definitivamente eso no es posible.

Pero lo podemos resolver de la siguiente manera. Cada contador por separado, según tu requerimiento las imágenes son 1,2,3,4 y 5, entonces i empieza en 1 y termina en 5.

Para el valor n, empieza en 6, con incrementos de 4.

Sub Insertar_Imagenes()
  Dim sPath As String, sFile As String
  Dim i As Long, n As Long
  Dim img As Object
  Dim rngCell As Range
  '
  sPath = Sheets("Main").Range("C11")
  n = 6
  '
  For i = 1 To 5
    Set rngCell = Worksheets("Sheet1").Range("B" & n)
    sFile = sPath & i & ".jpg"
    Set img = Worksheets("Sheet1").Pictures.Insert(sFile)
    With img
      .Left = rngCell.Left
      .Top = rngCell.Top
      .Width = rngCell.Width
      .Height = rngCell.Height
      .Placement = 1
      .PrintObject = True
    End With
    n = n + 4
  Next i
End Sub

Prueba y me comentas.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas