VBA Loop

hezcal11

Board Regular
Joined
Jan 20, 2012
Messages
57
Hello, I am try to go through a number of worsheets, unhide them, clear certain cell ranges and hide them again. I was trying the following macro but I seem to get an error and the routine stops. Can anyone suggest why.

but I seem to get an error and the routine stops. Can anyone suggest why.

Dim i As Integer
For i = 1 To 50
Worksheets(i).Visible = xlSheetVisible
Worksheets(i).Select
Range("B12:H12,B16:H16,B20:H20,B24:H24,B28:H28").Select
Selection.ClearContents
Range("b12").Select
ActiveSheet.Visible = xlVeryHidden
Range("w7") = i
Next i

End Sub

The line ACTIVESHEET.VISIBLE=XLVERY HIDDEN is highlighted yellow. It seems to work but stops on the sixth cycle.

Thanks

Colin
 
Hello, I still have the same issue. Its seems to work ok but I cannot understand why it even goes to the MAIN worksheet in the first place. And it's only certain lines it deletes but always the same ones. Is there any way to run a macro one stage at a time so that I could maybe see where it goes astray. Colin
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Using F8, there does not appear to be any deviation from the normal routine. It is deleting 6,10,14,18 and 22 on the MAIN worksheet which corresponds to the ranges identified in the macro for the pages 1 to 50. There is not much in the routine which could be going wrong.
 
Upvote 0
Maybe

Code:
Sub etest()
Dim i As Long
For i = 1 To 50
    If Worksheets(i).Name <> "MAIN" Then Worksheets(i).Range("B12:H12,B16:H16,B20:H20,B24:H24,B28:H28").ClearContents
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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