2017-09-12 8 views
0
私はExcelで、次のしている

は、以下の機能が今 ELSEIFは、

Private Sub CommandButton1_Click() 

    Dim i     As Integer 
    Dim value_s   As String 

    i = 2 
    Do Until IsEmpty(Cells(i, 1)) 
     value_s = ActiveSheet.Cells(i, 2) 

     If value_s <> "" Then 
      Debug.Print "value_s is not empty" 
     ElseIf value_s = "1200" Then 
      Debug.Print "value_s contains a 1200" 
     Else 
      Debug.Print "print the rest" 
     End If 

    i = i + 1 
    Loop 

End Sub 

を書かれている

+----------+-------+---------------+ 
| Option | value | VisibleOption | 
+----------+-------+---------------+ 
| Option#1 |  | #Option5  | 
| Option#2 |  | #Option6  | 
| Option#3 |  3 | #Option7  | 
| Option#4 | 1200 | #Option8  | 
+----------+-------+---------------+ 

ファイルをスキップする機能は、次の

print the rest 
print the rest 
value_s is not empty 
value_s is not empty 

ことながら返します1200が存在するので、次のような値を返します。

print the rest 
print the rest 
value_s contains a 1200 
value_s is not empty 
value_s is not empty 

ElseIfはスキップされているようです。ここで何がうまくいかない?

+0

intを文字列と比較しています – Tom

答えて

1

インスタンスで、それはそれをスキップ思われる場合には、値が空になる場合にのみ、elseifをするためになるだろう...それはそれを呼び出すことはない、if文は、最初の成功の後に閉じた場合。この正確な例を修正するには、私のような周りのものを切り替えます。これを行うための

If value_s == "" Then 
     Debug.Print "print the rest" 
    ElseIf value_s = "1200" Then 
     Debug.Print "value_s contains a 1200" 
    Else 
     Debug.Print "value_s is not empty" 
    End If 

より良い方法は、次のようになります。あなたが望むようにあなたに多くの可能性を追加することができます。この場合

Select Case value_s 
    Case "" 
     Debug.Print "print the rest" 
    Case "1200" 
     Debug.Print "value_s contains a 1200" 
    Case Else 
     Debug.Print "value_s is not empty" 
End Select 

0

は、あなたのループにこれを追加します。

debug.print ActiveSheet.Name 
debug.print value_s 

あなたはすぐにウィンドウに結果を得るでしょうし、あなたがそれに応じて修正することができます。

1

私はこのようなElseIf命令を書いていますが、私は1200が数字として格納されていると考えて、同じではない文字列 "1200"と比較します。

ElseIf value_s = 1200 Then 
+0

こんにちは、これを試してDim value_sを整数に変更し、Excelの列をnumberに変更しました。これらの変更により、タイプの不一致エラーが発生しました – user2237168