简体   繁体   中英

End Loop in Excel VBA

I have a excel VBA for a workbook. How do I end a loop on the worksheets? I'm not sure on how to do that. Here is the code.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
 Dim dtDate As Date
 Dim intHours As Long
 Dim ws As Worksheet

 intHours = 11
 dtDate = InputBox("Date", , Date)

 For Each ws In ThisWorkbook.Worksheets
 Set SelRange = Range("A6:A366")
 Next ws(**This where I need the loop to stop after the last worksheet**)

 For Each b In SelRange.Rows
 b.Value = dtDate + TimeSerial(intHours, 0, 0)
 b.Value = dtDate + TimeSerial(intHours, intMinutes, 0)
 intHours = intHours
 intMinutes = intMinutes + 1
 If intHours > 24 Then
        intHours = intHours - 24

End If
Next
End Sub

I need the loop to end after the last worksheet which is worksheet 6.

Per your question you just need to check the worksheet index to see if it is 6 and if so then exit the for loop. See below. In regards to your comments; you need to change this to the on workbook open method to only run it once when the workbookis opened.

On a side note, your first FOR loop is out of the scope of the second FOR loop so you are just setting the range over and over and doing nothing with it until the first FOR loop quits. It may be better to explain what you are trying to accomplish over all so you get a better response.

Private Sub Workbook_Open()
Dim dtDate As Date
Dim intHours As Long
Dim ws As Worksheet

intHours = 11

For Each ws In ThisWorkbook.Worksheets
    'check the index of the worksheet and exit if it is 6
    If ws.Index = 6 Then
        Exit For
    End If
'get the date per sheet
dtDate = InputBox("Date", , Date)
    Set SelRange = Range("A6:A366")
Next ws '(**This where I need the loop to stop after the last worksheet**)

For Each b In SelRange.Rows
    b.Value = dtDate + TimeSerial(intHours, 0, 0)
    b.Value = dtDate + TimeSerial(intHours, intMinutes, 0)
    intHours = intHours
    intMinutes = intMinutes + 1
    If intHours > 24 Then
       intHours = intHours - 24
    End If
Next
End Sub

This is what I think you are looking to accomplish.

Private Sub Workbook_Open()
Dim dtDate As Date
Dim intHours As Long
Dim ws As Worksheet

intHours = 11

For Each ws In ThisWorkbook.Worksheets

dtDate = InputBox("Date", , Date)
    'check the index of the worksheet and exit if it is 6
    If ws.Index = 6 Then
        Exit For
    End If
    Set SelRange = ws.Range("A6:A366")
    For Each b In SelRange.Rows
        b.Value = dtDate + TimeSerial(intHours, 0, 0)
        b.Value = dtDate + TimeSerial(intHours, intMinutes, 0)
        intHours = intHours
        intMinutes = intMinutes + 1
        If intHours > 24 Then
           intHours = intHours - 24
        End If
    Next
Next ws '(**This where I need the loop to stop after the last worksheet**)


End Sub

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