简体   繁体   中英

VBA macro stuck in loop

I have coded a (what I thought was simple) macro to repeat the same actions across 44 sheets of my workbook (while excluding the first 5).

Here is the code:

Sub LoopThroughSheets()
Dim ws As Worksheet
Dim Lastrow As Long
Dim i As Long
Dim rng As Range


For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Group Key" And ws.Name <> "AUX Key" And ws.Name <> "Start" And ws.Name <> "SSummary" And ws.Name <> "TSummary" Then
           'code omitted, but it essentially is adding a date column and a numbered row column in each worksheet/basic formatting     
    End If

Next ws

End Sub

How would I add a snippet of code that ends the loop once the last sheet (named "Tech") has been formatted?

I think you'd just need to add:

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Group Key" And ws.Name <> "AUX Key" And ws.Name <> "Start" And ws.Name <> "SSummary" And ws.Name <> "TSummary" Then
           'code omitted, but it essentially is adding a date column and a numbered row column in each worksheet/basic formatting     
    End If
    If ws.Name = "Tech" Then Exit For '' THIS LINE
Next ws

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM