1

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.

6
  • The loop will end at the last worksheet, but you are not using them correctly. What are you trying to do here?? The code has more than one problem.... Commented Dec 12, 2012 at 17:06
  • Doesn't the "For Each ws in ThisWorkbook.worksheets / Next ws" stop after the last worksheet? Commented Dec 12, 2012 at 17:06
  • If you need to exit before the last worksheet then you'd use Exit For Commented Dec 12, 2012 at 17:11
  • @JohnBustos I want the date prompt to come up when the workbook opens. That works...only problem is it will keep prompting me when I change worksheets...I only want it to prompt once when the workbook is open...What other problems do you see? Commented Dec 12, 2012 at 17:16
  • @TimWilliams at what part of the code would I apply that at? Commented Dec 12, 2012 at 17:19

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help....You are correct I need to explain more...I would like the date to prompt for each sheet in this workbook...currently when I change it to Workbook_Open it only prompts for the first sheet...How do I get it to prompt for the others?
@TankTank You just need to move the input box inside the FOR loop and after the Sheet.Index check. I have updated the code to reflect this. I think you are still going to have an issue with your two FOR loops being seperated.
you are right I am having problems with the FOR Loop being seperated...Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.