Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping round cells in a worksheet to add a shape to each cell using VBA

Tags:

loops

vba

I'm fairly new to VBA, and am trying to add an arrow to every second column in a table. I am getting the error: method 'range' of object '_global' failed.

What should I do in order to fix it.

Sub loop1()
    'Loop round range P6:AA10
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer


    For i = 9 To 14
        For j = 6 To 10
            k = (i * 2) - 1
            ActiveSheet.Shapes.AddShape(msoShapeRightArrow, Range(Cells(j, k)).Left + 2, _
                Range(Cells(j, k)).Top + 3, 15, 10).Select
        Next j
    Next i
End Sub
like image 417
Rolo Avatar asked Nov 13 '22 17:11

Rolo


1 Answers

Drop the Range(), looks like .Left and .Top are properties of Cells not Range objects. This code runs for me on Excel 2010:

Sub loop1()
    'Loop round range P6:AA10
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer


    For i = 9 To 14
        For j = 6 To 10
            k = (i * 2) - 1
            ActiveSheet.Shapes.AddShape(msoShapeRightArrow, Cells(j, k).Left + 2, _
                Cells(j, k).Top + 3, 15, 10).Select
        Next j
    Next i
End Sub
like image 174
Dan Avatar answered Jan 04 '23 01:01

Dan