2017-04-21 11 views
0

これは接続先Find data in a column from a cell reference in another worksheet then copy some data to another worksheet 特定の月に卒業した場合、クラス名を見つける必要があります。検索の参照に使用される月は別のシートにあります。最初に列Gで卒業月を検索し、検索で何も見つからない場合は次の列で検索します。 これは私がこれまで行ってきたことですが、これは1つの列のみを検索します。私はまだ他の列を検索する方法についてはわかりません。検索は、値が見つかる最初のインスタンスを停止する必要があります。vba列を介してセルデータを検索し、次の列を見ない場合

Sub Hierarchy() 

Dim shOUT As Worksheet 
Dim nfo As Worksheet 
Dim Month As Range 

Dim a As Long 
Dim thisvalue As String 

Set nfo = Worksheets("Info") 
Set shOUT = Worksheets("Output") 
Set Month = nfo.Range("A2") 

With shOUT 
     ' Loop through each row 
     For a = 2 To 10 
      thisvalue = .Cells(a, 7).Value 
      If thisvalue Like Month.Value Then ' <-- check the names 

      nfo.Range("A5").Value = .Cells(a, 2).Value 
      nfo.Range("A6").Value = .Cells(1, 7).Value 

      End If 
     Next a 
End With 

End Sub 
+1

if文に 'GoTo'を追加するだけです。あなたがそれを使用する方法を知らない場合は、Google GoTo in VBA – Masoud

答えて

1

私は余分なループを数える、プラスいくつかのマイナーな編集は十分かもしれません:

For b = 7 To 8 
    For a = 2 To 10 
     thisvalue = .Cells(a, b).Value 
     If thisvalue Like Month.Value Then ' <-- check the names 
      nfo.Range("A5").Value = .Cells(a, 2).Value 
      nfo.Range("A6").Value = .Cells(1, b).Value 
      Finished = True 
      Exit For '<-- skip the rest, we're done 
     End If 
    Next a 
    If Finished = True Then Exit For 
Next b 

(私はA5とA6の更新に関するあまりよく分からないとして)それはいくつかの調整が必要になることがありますが、概念は健全です。

宣言する必要がある変数(b、Finished)を2つ追加しました。

+0

私はbをLongとFinishedをBooleanと宣言しましたが、今度は次のエラーになります。 –

+0

こんにちはスティーブ、助けてくれてありがとう。 If Finishedステートメントを閉じるためにEnd Ifを追加したことを知らせたいと思っていました。 –

+0

ああ、そうです。私は 'If-Then'を複数の行に分けなければならないと思います。すべてが1行にある場合、 'End If'は必要ありません。 –

関連する問題