【问题标题】:End Loop in Excel VBAExcel VBA中的结束循环
【发布时间】:2012-12-13 03:30:49
【问题描述】:

我有一个用于工作簿的 excel VBA。如何结束工作表上的循环?我不确定该怎么做。这是代码。

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

我需要在最后一个工作表(即工作表 6)之后结束循环。

【问题讨论】:

  • 循环将在最后一个工作表处结束,但您没有正确使用它们。你想在这里做什么?代码有不止一个问题......
  • “For Each ws in ThisWorkbook.worksheets / Next ws”不是在最后一个工作表之后停止吗?
  • 如果您需要在最后一个工作表之前退出,那么您可以使用Exit For
  • @JohnBus​​tos 我希望在工作簿打开时出现日期提示。这行得通...唯一的问题是它会在我更改工作表时不断提示我...我只希望它在工作簿打开时提示一次...您还看到什么其他问题?
  • @TimWilliams 我会在代码的哪一部分应用它?

标签: excel vba


【解决方案1】:

根据您的问题,您只需要检查工作表索引以查看它是否为 6,如果是,则退出 for 循环。见下文。关于您的 cmets;您需要将其更改为 on workbook open 方法,以便仅在打开工作簿时运行一次。

附带说明,您的第一个 FOR 循环超出了第二个 FOR 循环的范围,因此您只是一遍又一遍地设置范围,并且在第一个 FOR 循环退出之前什么都不做。最好解释一下您要完成的全部工作,以便获得更好的响应。

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

这就是我认为你想要实现的目标。

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

【讨论】:

  • 感谢您的帮助....您是对的我需要解释更多...我希望在此工作簿中提示每个工作表的日期...当前当我将其更改为 Workbook_Open它只提示第一张...我如何让它提示其他人?
  • @TankTank 您只需要在 FOR 循环内和 Sheet.Index 检查之后移动输入框。我已经更新了代码以反映这一点。我认为您仍然会遇到两个 FOR 循环被分开的问题。
  • 你说得对,我在分离 FOR 循环时遇到问题...感谢您的帮助。
猜你喜欢
  • 2015-07-23
  • 1970-01-01
  • 2016-04-19
  • 2012-05-30
  • 2023-03-30
  • 2016-08-24
  • 2017-05-26
  • 2023-03-29
相关资源
最近更新 更多