For循环中的Select Case溢出错误

时间:2016-07-20 00:35:23

标签: excel vba excel-vba

我是一名CAD工程师,对VBA的知识相对较少,但今天我决定修改一个已有5年历史的程序,并遇到了这个问题:

我有一个组合框可以显示来自2个不同工作表的项目,具体取决于选择了哪两个无线电框。 这适用于案例。老实说,我不知道案件是如何工作的,所以我不知道组合框如何知道要采取哪种情况,但它有效,所以我不会改变它。

现在,目前代码是这样的:

这意味着如果我在表中添加一个新行,我必须复制那些案例选择

Private Sub ComboBox5_Change()                        'Auswahl Rohr

    Select Case ComboBox5.Value
        Case Tabelle6.Cells(4, 3)                     'Welle in Rohr

            LängRo = Tabelle6.Cells(4, 6)
            OnCenRo = Tabelle6.Cells(4, 7)
            OffCeRo = Tabelle6.Cells(4, 8)
            MinRRo = Tabelle6.Cells(4, 9)

        Case Tabelle13.Cells(4, 3)                    'ROHR IN ROHR

            LängRo = Tabelle13.Cells(4, 6)
            OnCenRo = Tabelle13.Cells(4, 7)
            OffCeRo = Tabelle13.Cells(4, 8)
            MinRRo = Tabelle13.Cells(4, 9)

        Case Tabelle13.Cells(5, 3)                    'ROHR IN ROHR

            LängRo = Tabelle13.Cells(5, 6)
            OnCenRo = Tabelle13.Cells(5, 7)
            OffCeRo = Tabelle13.Cells(5, 8)
            MinRRo = Tabelle13.Cells(5, 9)

    End Select

End Sub

我试图通过以下方式解决这个问题:

Private Sub ComboBox5_Change()                        'Auswahl Rohr

    Dim for1 As Long
    Dim for2 As Long
    For for2 = 3 To 7
        If Tabelle6.Cells(for2, 3) = "" Then
            GoTo fert
        Else
            Select Case ComboBox5.Value
                Case Tabelle6.Cells(for2, 3)          'Welle in Rohr

                    LängRo = Tabelle6.Cells(for2, 6)
                    OnCenRo = Tabelle6.Cells(for2, 7)
                    OffCeRo = Tabelle6.Cells(for2, 8)
                    MinRRo = Tabelle6.Cells(for2, 9)

            End Select
        End If
    Next

    For for1 = 3 To 7
        If Tabelle10.Cells(for1, 3) = "" Then
            GoTo fert
        Else
            Select Case ComboBox5.Value
                Case Tabelle10.Cells(for1, 3)         'ROHR IN ROHR

                    LängWe = Tabelle10.Cells(for1, 6)
                    OnCenWe = Tabelle10.Cells(for1, 7)
                    OffCeWe = Tabelle10.Cells(for1, 8)
                    MinRWe = Tabelle10.Cells(for1, 9)

            End Select
        End If
    Next

fert:

End Sub

但是现在当按下使用var LängRO的按钮时出现溢出错误,我真的不知道为什么

1 个答案:

答案 0 :(得分:0)

未测试:

Private Sub ComboBox5_Change() 'Auswahl Rohr

    Dim v, arrRows, c

    v = ComboBox5.Value

    arrRows = Array(Tabelle6.Cells(4, 3), _
                    Tabelle13.Cells(4, 3), _
                    Tabelle13.Cells(5, 3))

    For Each c In arrRows

        If c.Value = v Then
            'Found a match - assign from that row...
            With c.EntireRow
                LängRo = .Cells(6)
                OnCenRo = .Cells(7)
                OffCeRo = .Cells(8)
                MinRRo = .Cells(9)
            End With
            Exit For  'stop looping
        End If

    Next c

End Sub

我不确定我是否完全遵循了你的布局......