2017-05-09 136 views
0

我正在檢查一個值是否存在於一個範圍內。如果它不存在,那麼我希望它跳到WriteProcess否則我希望它給出一個消息框,說它存在並退出子。檢查一個值是否存在於一個範圍內或不與VBA

這是代碼,

'Write the Selected Value in the Range - Next Available row in the Column of Source 
    For i = TableStartingRow + 1 To AddNewEntrow   
     If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then 
      MsgBox "The data exists in the Table" 
      GoTo StopSub  
     Else 
      GoTo WriteProcess 
     End If 
    Next 

WriteProcess: 
    wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value 

StopSub: 
    'Turn on the ScreenUpdate 
    Application.ScreenUpdating = True 

請分享你的看法。謝謝。

+2

這裏有個問題嗎?代碼片段看起來像它會工作得很好。你有錯誤嗎? – tigeravatar

+0

似乎更好地使用'Find'方法或'Application.Match'函數的範圍,但我沒有看到這段代碼有什麼問題。請詳細說明。 –

+0

@tigeravatar是的,先生。它工作正常。但它沒有做我想做的事。當我在調試模式下運行代碼時,我發現它在運行增量i中的所有值之前運行了else循環。我希望代碼檢查增量i中的所有值,只有當它們不匹配所有這些值時,它才能進入WriteProcess。 – Sid29

回答

3

如果您需要檢查前夕的備用解決方案ry row執行「WriteProcess」之前:

Dim bExists As Boolean 

    bExists = False 
    'Write the Selected Value in the Range - Next Available row in the Column of Source 
    For i = TableStartingRow + 1 To AddNewEntrow 
     If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then 
      bExists = True 
      MsgBox "The data exists in the Table" 
      Exit For 
     End If 
    Next 

    If Not bExists Then wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value 

    'Turn on the ScreenUpdate 
    Application.ScreenUpdating = True 
4

你的問題是,如果循環過期(耗盡所有的迭代),沒有控制來防止它進入WriteProcess。

這是使用GoTo語句時的一個問題。最好將這些降到最低。例如,雖然這並不檢查每行行,只是一個例子說明如何避免額外的GoTo

'Write the Selected Value in the Range - Next Available row in the Column of Source 
    For i = TableStartingRow + 1 To AddNewEntrow   
     If Range(EntryColLett & i).Value = wb21Tool.Sheets("Home").ComboBox1.Value Then 
      MsgBox "The data exists in the Table" 
      GoTo StopSub  
     Else 
      wbTool.Sheets("Home").Range(EntryColLett & AddNewEntrow).Value = wb21Tool.Sheets("Home").ComboBox1.Value 
     End If 
    Next 

StopSub: 
    'Turn on the ScreenUpdate 
    Application.ScreenUpdating = True 

但是,蠻力遍歷所有表中的數據似乎沒有必要,如果你需要檢查所有行詮釋,他的表就可能更好地只使用Find方法。

假設EntryColLet是代表列字母的字符串:

Dim tblRange as Range 
    Dim foundRow as Range 
    Set tblRange = Range(EntryColLet & (TableStartingRow+1) & ":" & EntryColLet & AddNewEntRow) 
    Set foundRow = tblRange.Find(wb21Tool.Sheets("Home").ComboBox1.Value) 
    If foundRow Is Nothing Then 
     'The value doesn't exist in the table, so do something 
     ' 
    Else 
     'The value exists already 
     MsgBox "The data exists in the Table" 
     GoTo StopSub 
    End If 

    'More code, if you have any... 

StopSub: 
    Application.ScreenUpdating = True 

而對於其餘GoTo - 如果沒有更多的代碼,條件If foundRow Is Nothing後執行,那麼你可以刪除整個Else條款GoTo標籤:

Dim tblRange as Range 
    Dim foundRow as Range 
    Set tblRange = Range(EntryColLet & (TableStartingRow+1) & ":" & EntryColLet & AddNewEntRow) 
    Set foundRow = tblRange.Find(wb21Tool.Sheets("Home").ComboBox1.Value) 
    If foundRow Is Nothing Then 
     'The value doesn't exist in the table, so do something 
    End If 
    Application.ScreenUpdating = True 
End Sub 
+0

我明白了,我去錯了先生。謝謝。但我的要求仍然不匹配。謝謝。 – Sid29

+0

Found Row方法適用於我。謝謝:) – Sid29

+2

我完全同意你的建議,使用'範圍。Find'方法比遍歷每個單元格要好得多 – tigeravatar

相關問題