For Each inside another loop works only once after excel startup

shokarta

New Member
Joined
Nov 20, 2017
Messages
2
Hello guys,

I have an macro I am fighting last 5 days with no luck.

I am doing FOR loop every row filled and hidding shapes (pictures) based on value in Column C in every row.
So far I am only in stage that I wish to FOR loop of all shapes and hide or show (just to test that everything works fine so far).

The issue is, that the macro works fine the first time I run it, and if I run it again, I got an error... When I close excel and open again, then I can run the macro once only :/.

Code:
Sub ds()

    Dim lastrow As Long
    lastrow = Worksheets("Task_List").Cells(Rows.Count, 1).End(xlUp).Row


    Dim ShapeAction As Shape


    Dim i As Integer
    For i = 6 To lastrow


        For Each ShapeAction In Worksheets("Task_List").Shapes
            If ShapeAction.TopLeftCell.Row = i And ShapeAction.TopLeftCell.Column = 14 Then
                MsgBox ShapeAction.TopLeftCell.Row & "-" & ShapeAction.TopLeftCell.Column
            End If
            If ShapeAction.TopLeftCell.Row = i And ShapeAction.TopLeftCell.Column = 15 Then
                MsgBox ShapeAction.TopLeftCell.Row & "-" & ShapeAction.TopLeftCell.Column
            End If


        Next ShapeAction
    Next i


End Sub

In these past 5 days I have tried everything I could posibly find out on the internet, the different kind of IF conditions, such for example If Not Intersect(Range(Cells(i, 14), ActionShape.TopLeftCell) Is Nothing Then (and so many other ways to find shape in a cell.

Getting always same error on IF statement.

See images as errors
UJ74Y.png

AWuVl.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello Guys,

I am lot of frustrated with this... I dont understand the behaviour.

Sometimes I isert new row and it works, another time it doesnt work.
Then Sometimes helps if I save the excel first before run the macro.

It is like something is keeping in cache, I tried to:
Set ShapeAction = Notning before Next ShapeAction but it didnt help...

Sometimes when the sheet changed (random cell which was before empty is filled now) works, sometimes not :/
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top