2016-07-20 163 views
1

我是一个CAD工程师VBA的relativly小知识选择案例,但今天溢出错误,我决定修改一个5岁的程序和过这样的问题来了:在For循环

我有一个组合框是可以显示来自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一个按钮,我真的不知道为什么

+0

你并不需要的情况下语句分开,情况就是将它们分开。你在哪里得到错误?溢出是正常情况下,当您试图将大量或错误的数据放入变量中时。 – MatthewD

+0

嘿马修我认为我只是去与蒂姆的代码,它简化了问题,因为它不是我的代码,我认为它可能需要很长时间,直到我找到导致溢出的代码中的“正确”的错误。 – Tom

回答

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 

我不知道我跟着你的布局是什么,但...

+0

嗨蒂姆 非常感谢,这简化了问题,因为我只需要为每个新项目添加一条新线,非常感谢! 干杯 – Tom