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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try

Code:
Sub etest()
Dim i As Long
For i = 1 To 50
    Worksheets(i).Range("B12:H12,B16:H16,B20:H20,B24:H24,B28:H28").ClearContents
Next i
End Sub
 
Upvote 0
Does this help?
Code:
    Dim i As Integer
    For i = 1 To ActiveWorkbook.Sheets.Count
          Worksheets(i).Range("B12:H12,B16:H16,B20:H20,B24:H24,B28:H28")_
          .ClearContents
          Worksheets(i).Range("W7") = i
    Next i
 
Upvote 0
I agree with HTH Peter that it should be "as Long" not "as Integer". If you do not want to do anything to the main worksheet then just disqualify that sheet.
 
Upvote 0
Sorry, I did not get a chance. I was at work when I sent the post and got sidetracked. I appreciate your reply and will try it this morning. I did not intend to appear rude.

Colin
 
Upvote 0
Thanks, this works to 50 as well. However, I still have the same issue that lines on the main worksheet are deleted during the routine.
 
Upvote 0
Try

Code:
Sub etest()
Dim i As Long
For i = 1 To 50
    If Worksheets(i).Visible <> xlSheetVisible 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,213,560
Messages
6,114,306
Members
448,564
Latest member
ED38

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