2016-12-19 9 views
3

私はコードでGoToを避けるべきであると何度も読んでいますが、私は定期的にループを作成しなければならず、アイテムによって私にエラーが生じた場合、コードは停止します。VBAを避けるには?

以下のケースでは、セルの値を比較する必要がありますが、セルに文字列があるとエラーが発生します。

次のコードのGoToを避ける他のオプションはありますか?

ありがとうございます!この場合

Sub Conditional() 

     Dim x As Integer 



     For x = 1 To 31 



      On Error GoTo na 


      If Sheets("Sheet1").Cells(8 + x, 5) >= 0.95 Then 

       Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(0, 176, 80) 

      ElseIf Sheets("Sheet1").Cells(8 + x, 5) < 0.95 Then 

       Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(255, 0, 0) 

      End If 

      GoTo nextx 

na: 

      Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(0, 0, 0) 
      On Error GoTo -1 

nextx: 
     Next x 

End Sub 
+0

適切なエラートラッピング/処理が最適なオプションかもしれません。 –

答えて

3

あなたはそれがこのようなものですセル内の文字列をチェックしたい場合は、一般に

Sub Conditional() 

    Dim x As Long 

    For x = 1 To 31 
     If IsNumeric(Sheets("Sheets1").Cells(8 + x, 5)) Then 
      If Sheets("Sheet1").Cells(8 + x, 5) >= 0.95 Then 

       Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(0, 176, 80) 

      ElseIf Sheets("Sheet1").Cells(8 + x, 5) < 0.95 Then 

       Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(255, 0, 0) 

      End If 
     End If 
    Next x 
End Sub 

、後藤は無いとOn Error GoTo ErrorHandlerのように、キャッチだけエラーのために使用すべきですexclustions。

+0

'IsNumeric()'は状況によっては失敗し、実際には数字でないものに対しては 'True'(="それは数字です) "を返します。例えば ​​'IsNumeric(" 1..23 ")'は 'True'を返します。 'SpecialCells(..、xlNumbers)'は_real_番号を返します。私の答えは – user3598756

+0

を参照してください '?isnumeric(range(" A3 "))'を試してみて、セルに '1..23'を置いたときの結果を教えてください。 – Vityata

+0

「True」を返します – user3598756

1

あなたは細胞タイプtextまたはnumbers)に応じて、いくつかの書式設定をやっているので、あなたはRangeオブジェクトのSpecialCells()メソッドを使用して機能を入力し、次のようなことができます:

Function GetCells(rng As Range, cellType As XlCellType, cellValues As XlSpecialCellsValue, outputRng As Range) As Boolean 
    On Error Resume Next '<--| ignore subsequent errors, if any 
    Set outputRng = rng.SpecialCells(cellType, cellValues) '<--| get a sub-range out of passed range as filtered by passed arguments 
    GetCells = Not outputRng Is Nothing '<--| returns 'True' if range has been properly set 
End Function 

を、次のように利用します:

Sub Conditional() 
    Dim myCells As Range, cell As Range 

    With Sheets("Sheet 1").Range("E9:E39") '<--| reference your sheet relevant range   
     If GetCells(.Cells, xlCellTypeConstants, xlNumbers, myCells) Then '<--| if any cells whose value is a "constant" "number" in your range 
      For Each cell In myCells '<--| loop through those filtered cells 
       Sheets("Sheet2").Shapes("Shape " & cell.row - 8).Fill.ForeColor.RGB = IIf(cell.Value >= 0.95, RGB(0, 176, 80), RGB(255, 0, 0)) '<--| format your shapes as per corresponding current cell 
      Next cell 
     End If 

     If GetCells(.Cells, xlCellTypeConstants, xlTextValues, myCells) Then '<--| if any cells whose value is a "constant" "text" in your range 
      Sheets("Sheet2").Shapes("Shape " & cell.row - 8).Fill.ForeColor.RGB = RGB(0, 0, 0) '<--| format your shapes as per corresponding current cell 
     End If 
    End With 
End Sub 
関連する問題