2016-09-12 2 views
0

こんにちは私は論理演算を行っていますか、そうではなく、そしてvbaとしていますが、私のコードに問題があります。助けていただければ幸いです。vbaで論理演算を行っています

sub logicop() 

Dim myRange As range 
Dim rowi As range 
Dim cell As range 

Set myRange = Range("A8:F20") 'go into each row of column 
For Each rowi In myeRange.Rows 
    andfunc = 1 'AND operaton 
    notfunc = 0 'NOT function 
    result1 = 0 
    result2 = 0 
    result3 = 0 
For Each cell In rowi.Cells 'go into each cell of the rows in rowi 

     If cell.Value = notfunc Then 
     resuslt1 = result1 + 1 
     End If 

     If cell.Value = andfunc Then 
     resuslt2 = result2 + 1 
     End If 

     If cell.Value <> andfunc And cell.Value <> notfun Then 
      result3 = result3 + 1 
      End If 

    Next cell 
Next 

    result1 = Cells(3,3) 
    result2 = Cells(3, 4) 
    result3 = Cells(3,5) 

End Sub 
+0

'notfun'は' notfunc'する必要があります。 (実際には未定義の変数 'notfun'はおそらく' notfunc'と同じ値である0の値を持つため、おそらく役に立たないでしょう) – YowE3K

+5

おそらく、エラーは 'myeRange.Rows' 'myRange.Rows'の代わりに使用されます。私はあなたのモジュールの始めにOption Explicitを含めることを強くお勧めします**。 – YowE3K

+2

'result1'と' result2'と 'result3'を3つの特定のセルに設定しようとしているときにループを邪魔するのはなぜですか? –

答えて

3

コメントに指摘されているように、コードフォームの動作を妨げる数多くのスペルミスがあります。これは、Option Explicitを使用して回避できます。

ここでは、コードのクリーンアップ版です。私は、空のセルを0として、それが書かれている方法をカウントされることSelect Case

Option Explicit 
Sub logicop() 
    Dim wks As Worksheet 
    Dim myRange As Range 
    Dim rowi As Range 
    Dim cell As Range 
    Dim andfunc, notfunc, result1, result2, result3 

    Set wks = Sheets("Sheet1") 
    Set myRange = wks.Range("A8:F20") 'go into each row of column 

    andfunc = 1 'AND operaton 
    notfunc = 0 'NOT function 
    result1 = 0 
    result2 = 0 
    result3 = 0 

    For Each rowi In myRange.Rows 
     For Each cell In rowi.Cells 'go into each cell of the rows in rowi 
      Select Case cell.Value 
       Case notfunc 
        result1 = result1 + 1 
       Case andfunc 
        result2 = result2 + 1 
       Case Else 
        result3 = result3 + 1 
      End Select 
     Next cell 
    Next rowi 

    'Output results to specific cells 
    wks.Cells(3, 3).Value = result1 
    wks.Cells(3, 4).Value = result2 
    wks.Cells(3, 5).Value = result3 
End Sub 

注記に追加の例外を除いて、あなたのオリジナルに近いそれを維持しようとしました。あなたの範囲内のすべてのセルにデータがあると仮定しているので、問題になることはありません。


例結果:コメントパー

enter image description here


EDIT

、私はすべて0、1のまたは混合を持っていた行をカウントするためのコードを更新しました。

Option Explicit 
Sub logicop() 
    Dim wks As Worksheet 
    Dim myRange As Range 
    Dim rowi As Range 
    Dim andfunc, notfunc, result1, result2, result3, rowSum 

    Set wks = Sheets("Sheet1") 
    Set myRange = wks.Range("A8:F20") 'go into each row of column 

    andfunc = 1 'AND operaton 
    notfunc = 0 'NOT function 
    result1 = 0 
    result2 = 0 
    result3 = 0 

    For Each rowi In myRange.Rows 
     rowSum = Application.WorksheetFunction.Sum(rowi) 

     Select Case rowSum/rowi.Cells.Count 
      Case notfunc 
       result1 = result1 + 1 
      Case andfunc 
       result2 = result2 + 1 
      Case Else 
       result3 = result3 + 1 
     End Select 
    Next rowi 

    'Output results to specific cells 
    wks.Cells(3, 3).Value = result1 
    wks.Cells(3, 4).Value = result2 
    wks.Cells(3, 5).Value = result3 
End Sub 

結果

enter image description here

+0

スペルミスのためにやってみようとしていましたが、私は自分のノートパソコンではありませんでした。私のコードを設定する方法は多分少し離れています。ケースの文章はセルの値を数えていますが、行の1つのセルのすべての値がすべて1であるならば論理演算を行いたいです。増分を続ける。 'Set myRange = Range(" A1:B3 ")' そして、1行目のすべての値は0としてカウントされますが、コードは3とカウントしているので、おそらくコードを設定する方法かもしれません。助言がありますか? – dfgool

+0

私は多分result1をnoで除算することを考えていました。私は 'wks.Cells(3、3).Value = result1/cell'を実行しようとしました。 しかし、間違っているのは私にはエラーがあります – dfgool

+0

@dfgool私の投稿を編集しましたあなたのコメントに基づいた別のコードセット。私はこれがあなたの探しているものだと思います。それを試して、私があなたを正しく理解しているかどうかを見てください。乾杯 –