Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:08:21+00:00 2026-06-15T20:08:21+00:00

I have a excel VBA for a workbook. How do I end a loop

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:08:23+00:00Added an answer on June 15, 2026 at 8:08 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel workbook, which using VBA code that opens another workbook, copies
I'm using Excel VBA and have a worksheet_change event like so: Private Sub Worksheet_Change(ByVal
I have a VB6/VBA application that hooks into Excel and loads a workbook. It
I have produced some VBA code to resolve this problem : Private Sub Workbook_SheetChange(ByVal
I have written some vba code that selects a range from a workbook and
I am have the following code for excel vba that will email a range
I have a VBA code that is copying a range in Excel and pasting
I have an unmanaged C# DLL that is added to an Excel VBA code.
I have an excel file with VBA macro that starts when the Workbook is
I have a working excel vba macro that does what I want from here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.