【问题标题】:For Loop Being Skipped OverFor 循环被跳过
【发布时间】:2015-03-04 20:41:13
【问题描述】:

我无法弄清楚为什么代码会跳过 For 循环块(有两个 for 循环)。我知道它被跳过了,因为消息框在代码中被进一步触发。有什么想法吗?

Dim OL As Outlook.Application
Dim myitem As Outlook.MailItem
Dim wb As Workbook
Dim ws As Worksheet
Dim PicPath As String
Dim x, y As Integer
Dim Name As Name
Dim cell, rng1 As Range


Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")

wb.Sheets("Sheet1").Activate

'Request type check
If (Sheet1.NewResource.Value = False) And (Sheet1.Modification.Value = False) Then
    MsgBox "Please select a Request Type of your request and complete the other mandatory fields"
    GoTo cont
End If

'checking New Resource Entry mandatory fields
If Sheet1.NewResource.Value = True Then

    If (Sheet1.NewPOnobx.Value = False) And (Sheet1.NewPOyesbx.Value = False) Then
        MsgBox "Please fill the missing mandatory fields"
        GoTo cont
    End If

    If (Sheet1.POyesbx.Value = False) And (Sheet1.POnobx.Value = False) Then
        MsgBox "Please fill the missing mandatory fields"
        GoTo cont
    End If

    If (Sheet1.POnobx.Value = True) And (Sheet1.SOWtxt.Value = "") Then
        MsgBox "Please select a copy of the Signed Agreement"
        GoTo cont
    End If

    For Each Name In ActiveWorkbook.Names 'each named range in gateway fields
        If (Name = "C_LN") Or (Name = "C_FN") Or (Name = "C_SD") Or (Name =  "C_ED") Or (Name = "C_O") Or (Name = "C_D") Or (Name = "C_OF") Or (Name = "C_C") Or (Name = "C_M") Or _
        (Name = "C_R") Or (Name = "C_PT") Or (Name = "C_V") Or (Name = "C_SAR") Or (Name = "C_BAR") Or (Name = "C_SR") Or (Name = "C_SS") Or (Name = "C_PO") Then 'all gateway field names
            x = 0
            For Each cell In Name 'cell in the named range
                If cell.Value = "" Then 'looking for blank mandatory fields
                    If (cell.Name = "LN_Cmt") Or (cell.Name = "FN_Cmt") Or (cell.Name = "SD_Cmt") Or (cell.Name = "ED_Cmt") Or (cell.Name = "O_Cmt") Or _
                    (cell.Name = "D_Cmt") Or (cell.Name = "OF_Cmt") Or (cell.Name = "C_Cmt") Or (cell.Name = "M_Cmt") Or (cell.Name = "R_Cmt") Or _
                    (cell.Name = "PT_Cmt") Or (cell.Name = "V_Cmt") Or (cell.Name = "SAR_Cmt") Or (cell.Name = "BAR_Cmt") Or (cell.Name = "SR_Cmt") Or _
                    (cell.Name = "SS_Cmt") Or (cell.Name = "PO_Cmt") Then
                        GoTo skip1
                    Else
                        x = x + 1 '+1 for a blank mandatory field
                    End If
                End If
skip1:
            Next cell

                If x > 0 Then 'flag the missing info
                    MsgBox "Please fill the missing mandatory fields"
                    GoTo cont
                End If

            End If
        Next Name

End If

【问题讨论】:

  • I know it's being skipped bc a message box - 这不好,请使用调试器。密钥是F8
  • GoTo Skip1 完成了什么?试着评论一下,看看事情是否更好。 (在 ELSE 之前有一个空白的 THEN 操作是合法的。) GoTo 语句可能会搞砸事情,而这个语句没有明显的目的。一定要使用调试器——把红色的停止标志放在左列,然后按 F8 t 单步执行你的代码,看看发生了什么。
  • Skip1 的目的是跳过 x 变量计数器并继续到下一个单元格。
  • 我更大的问题是代码甚至没有被拾取......我已经完成了调试器并在 ActiveWorkbook.Names 中的每个名称之后停止了它并没有在那里停止跨度>
  • 然后在上面放一个断点,看看为什么不满足进入循环的条件。 (在If Sheet1.NewResource.Value = True Then 处设置断点并从那里逐步执行代码以查看发生了什么。我们无法为您执行此操作,因为我们没有可用于执行此操作的电子表格。)

标签: excel vba


【解决方案1】:

对我来说,for 循环中的Name 给出了类似=Sheet1!$G$2:$G$5 的内容...这与您的C_R 或其他内容不匹配。我需要使用Name.Name 来获得它。然后要获取名称的单元格范围,我需要使用Name.RefersToRange

另外,为了让您的If 列表更易于管理,您可以改用Select Case

  Select Case Name.Name
    Case "C_LN", "C_FN", "C_SD",  "C_ED", "C_O", "C_D", "C_OF", "C_C", "C_M", "C_R", "C_PT", "C_V", "C_SAR", "C_BAR", "C_SR", "C_SS", "C_PO"
      For Each cell In Name.RefersToRange
        Debug.Print cell.Address
      Next cell
    Case Else
      Debug.Print "Other Name"
  End Select

【讨论】:

    【解决方案2】:

    不要使用名称,它是保留字(您可以将名称 File1 的文件重命名为 File2)。

    将变量名称更改为 MyName 或类似名称,然后如果您继续遇到问题,请停止代码并通过键入 ?activeworkbook.Names.Count 并在调试风 (CTRL-G) 中手动评估循环按回车键(别忘了?),看看你是否有一个大于零的数字。

    【讨论】:

    • Name 是一个上下文相关的关键字。它只是Name ... As ... 子句中的关键字,而不是其他任何地方的关键字。可以有一个名为Name 的变量。你甚至可以Name Name As Name
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多