私は大まかにフォーマットされたデータセットを扱います。私は必要なデータポイントを取得するために複数のFind
メソッドを使用しています。私のサンプルデータベースには、3つのデータ期間(すべて1枚のシート)があり、複数の役員名があります。名前は特定の期間内である場合とそうでない場合があります。私は変数officerSearch
を使用して、次のFind
メソッドのアンカーポイントとなるアドレスを格納しています。役人の名前が特定の期間内にない場合は、私のFind
メソッドはコースから降ります。私のコードコメントの詳細。Excel-VBAシートの最後の後にFINDメソッドがループバックしないようにするにはどうすればよいですか?
For Each k In dateArray.keys 'loops through my periods. Each key is a unique date
For i = 0 To officerListBox.ListCount - 1
If officerListBox.Selected(i) = True Then 'Performs _
Find methods for each officer selected in list box
officerSearch = Cells.Find(what:=CDate(k), LookIn:=xlFormulas, _
searchorder:=xlByRows, searchdirection:=xlNext).Address 'Finds first instance of the first period (k)
officerSearch = Cells.Find(what:=officerListBox.List(i), _
after:=Range(officerSearch), LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows, searchdirection:=xlNext).Address _
'Finds officer name starting after the date cell was found. _
But if the raw data set doesn't have the officer name in that period, _
my Find method will find an officer name further on down the sheet _
(or loop around at the beginning of the sheet) where the name is found in a different period.
officerSearch = Cells.Find(what:="Gross Loans", after:=Range(officerSearch), _
LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext).Address 'Finds the cell labeled Gross Loans starting after an officer name is found
officerSearch = Cells.Find(what:="Total 30 - 59 days past due", _
after:=Range(officerSearch), LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows, searchdirection:=xlNext).Address 'Finds the cell _
labeled Total 30-59 days past due starting after _
an officer name is found.
End If
Next i 'Starts the find loop over for the next selected officer name
Next k 'Starts the find loop over for the next period in the dataset
だから私の第一、第三、および第四のFindメソッドは、私が期待する場所に存在することが保証されていますが、第二Findメソッドは、私のループが探している期間内に配置されない場合があり、したがって、他のすべてを投げオフ。私はこれをどのように説明するのか困惑しています。
*あなたのコードの '.Find'メソッドのスコープが' Worksheet.Cells'、つまり**ワークシート全体**です。 2番目、3番目、4番目の 'Find'呼び出しの検索範囲を定義するより良い方法が必要です。なぜなら、現在書かれているように、2番目の' .Find'は、あなたの期間の最初のインスタンスを全体から検索しますその下のワークシート。 –
データは日付でソートされていますか? –
http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/ –