When using for each loop for delete worksheets ( showing error msg for completing the task

ManojRana

New Member
Joined
Jul 23, 2017
Messages
5
Sub ForEachLoop1()


For Each cl In Range("A1").CurrentRegion
cl.Select
If cl = 10 Then
cl.Interior.Color = vbRed
End If
Next cl
End Sub
Sub ForeachExample2()
Application.DisplayAlerts = True
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Delhi" Then

Else
ws.Delete

End If


Next ws
 

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)
Is this what you want:
Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 1 To Lastrow
        If Cells(i, 1).Value = "10" Then Cells(i, 1).Interior.Color = vbRed
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Is this what you want:

Code:
Sub ForeachExample_New()
Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name = "Delhi" Then ws.Delete
    Next
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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