2016-07-13 4 views
0

この問題は文章が多少奇妙であることを認識していますが、私は比較的新しくコーディングしています。 1つのシート上のデータの列を繰り返し、その列の各セルを、別のシートの特定の行を検索するための検索語として使用したいとします。明示的に定義されていないセルからデータを参照する

Sub Test1() 
n = 0 
' n is the counter for the number of times that the search term will appear 

Dim sn As String 
For Each a In Worksheets("Sheet1").range("A5:A34").Value 
f = a.Cells 
sn = range(a).Value 
' each sn is equal to the contents of the cell "a", which will be the serial number of the tooling 


For Each c In Worksheets("RunsheetTest").range("A3:R3") 
' Defines c as a range with cells A3 to R3? 


If InStr(c.Cells, sn) > 0 Then 
n = n + 1 
Else 
n = n 
End If 
' If the serial number is found in one of the cells, add 1 to n and move on to the next. 

Next c 

MsgBox "Searching for " & sn 
range(a).Value = n 
Next a 
' I'm also getting an error here, saying "for loop not initialized", however I thought I initialized it above. 

End Sub 

私の最大の問題は、私はループではなく、特定のセルの現在オンになっているセルを参照したいので、私は、一番上の「F = a.Cells」のために右の構文を使用しているかどうかであるとなぜコンパイラが私のループにエラーを与えているのですか? VBAでネストループを実行できませんか?また、私のコメントのどれかが実際に何が起こっているかについて全く正確でない場合は、私に知らせてください。

ありがとうございます!

+0

"f"とは何ですか?変数名で何をしようとしているのかを理解するのは難しい – RGA

+0

VBAの範囲とFor Eachループの動作を正しく参照する方法を調べることをお勧めします。あなたの一般的な構造は(コードの設定方法のように)健全ですが、構文/方法はすべてオフです – RGA

+0

私は@RGAと合意しています。 'a'、' c'、 'f'は変数の説明的な名前ではなく、最終的には混乱するでしょう。 :)私は個人的に、データ型の3文字の略語で各変数を開始します。例えば、シリアル番号変数の 'strSN'は文字列であるため、' a'の代わりに 'rngSearchTerm'であり、これは範囲であり、見つけたい値を含んでいるからです。 – Tim

答えて

0

これを試してください:あなたはrange(a)またはrange(c)でそれらを参照しないよう

Sub Test1() 
    Dim n As Integer 
    Dim a As Range 
    Dim c As Range 
    Dim sn As String 

    n = 0 
    ' n is the counter for the number of times that the search term will appear 

    For Each a In Worksheets("Sheet1").Range("A5:A34") 
     'f = a.Cells 
     sn = a.Value 
     ' each sn is equal to the contents of the cell "a", which will be the serial number of the tooling 

     For Each c In Worksheets("RunsheetTest").Range("A3:R3") 
      ' Defines c as a range with cells A3 to R3? 
      If InStr(1, c.Value, sn, vbTextCompare) > 0 Then 
       n = n + 1 
      Else 
       n = n 
      End If 
      ' If the serial number is found in one of the cells, add 1 to n and move on to the next. 
     Next 

     MsgBox "Searching for " & sn 
     a.Value = n 
    Next 
    ' I'm also getting an error here, saying "for loop not initialized", however I thought I initialized it above. 

End Sub 

acは、範囲です。また、ではなく、Next aを使用してください。

+0

の多くを保存しますそれぞれの作業のための最初のだろうか?あなたは "a"が範囲だと言っていますが、値をループしていますか? – RGA

+1

@RGAいいキャッチ!私は実際にコードをテストしていない、ちょうどOPのコードの間違いを修正しました。私はそれを編集します。 – Tim

+0

@Tim - OPのために 'if'の' else'部分が冗長であることを書きたいと思っていました。彼はコードがどのように変更されたかを理解した後、削除することができます。 –

0

もう1つ質問してください - VBAはネストされたループをサポートしていますが、組み込みのRange.Find()メソッドを使用する方がよいでしょう。以下の例と私のコメントを参照してください。

Sub Test1() 

    Dim n As Long 
    n = 0 ' n is the counter for the number of times that the search term will appear 

    Dim sn As String 
    Dim c As Range 
    Dim a As Range 

    '' removing ".Value" at the end -- you need to loop through a range 
    For Each a In Worksheets("Sheet1").Range("A5:A34") 
     '' here "a" is already a range object that contains one cell so all you need is: 
     n = 0 '' reset the counter for new serial number 
     sn = a 

     '' to search for a value it is better to use special .Find function on a range, not a loop 
     With Worksheets("RunsheetTest").Range("A3:R3") 

      Set c = .Find(sn, LookIn:=xlValues) 
      If Not c Is Nothing Then 
       '' you get here only if something is found, otherwise counter will be 0 -- i.e. nothing found 
       firstAddress = c.Address 
       Do 
        n = n + 1 
        Set c = .FindNext(c) 
       Loop While Not c Is Nothing And c.Address <> firstAddress 
      End If 

     End With 

     MsgBox "Searching for " & sn & " and found it " & n & " times." 

    Next a 

End Sub 
関連する問題