0

How can I put this code in a cycle? For example start the cylce in the A3 cell, end of the cycle in A100 cell.

Sub CenterImages()
        With ActiveSheet.Shapes("Picture 1")
            .Top = Range("A1").Top + (Range("A1").Height - .Height) / 2
            .Left = Range("A1").Left + (Range("A1").Width - .Width) / 2
        End With
End Sub

1 Answer 1

2

Do something like that:

Option Explicit

Public Sub CenterImages()
    Dim i As Long
    For i = 3 To 100
        CenterImage i
    Next i
End Sub

Public Sub CenterImage(ByVal Index As Long)
    With ActiveSheet.Shapes("Picture " & Index)
        .Top = Range("A" & Index).Top + (Range("A" & Index).Height - .Height) / 2
        .Left = Range("A" & Index).Left + (Range("A" & Index).Width - .Width) / 2
    End With
End Sub

Make your code generic by using an Index then call your procedure in a loop from 3 to 100 specifying that index.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.