限定しりとり

いつもお世話になっております。
下記のコードは
B列に
ABC AACという文字を
すべて選択するというコードを作成しましたが、
以下のコードを実行すると
最終行だけ選択されます。
わかる方おしえてくれませんでしょうか


Dim myRng As Range, c As Range, Target As Range, i As Long

Set myRng = Range("B1", Cells(Rows.Count, "B").End(xlUp))

For Each myStr In Array("ABC", "AAC")

Set c = myRng.Find(What:=myStr, LookAt:=xlPart, After:=myRng.Cells(myRng.Count))

If Not c Is Nothing Then
Set Target = c
Else
Set Target = Union(Target, c)
End If
Next

If Not Target Is Nothing Then
Target.Select
End If

質問者からの補足コメント

  • うーん・・・

    下記のコードは問題なく全選択されます。
    ただ、木の場合だと 条件1つだけてす
    For Each を利用してやりたいのですが、
    Dim Target As Range
    For i = 134 To 137
    If InStr(Cells(i, 1).Value, "Pro_VBA") > 0 Then
    If Target Is Nothing Then
    Set Target = Cells(i, 1)
    Else
    Set Target = Union(Target, Cells(i, 1))
    End If
    End If
    Next
    If Not Target Is Nothing Then
    Target.Select
    End If
    End Sub

      補足日時:2020/09/08 11:02
  • うーん・・・

    If InStr(Cells(i, 1).Value, "Pro_VBA") > 0
    Or
    InStr(Cells(i, 1).Value, "End Sub") > 0 Then
    でいきますが、For Eachでおしえてくれませんでしょうか

      補足日時:2020/09/08 11:06
  • うーん・・・

    FindNextメソッドですか。
    やってみます。
    ありがとうございます。

      補足日時:2020/09/08 11:27
  • うーん・・・

    For Each mystr In Array("ABC", "AAC")
    Set c = myRng.Find(What:=mystr, LookAt:=xlPart, After:=myRng.Cells(myRng.Count)) '〇
    Next
    If Not c Is Nothing Then
    Set Target = c
    Else
    Do
    Set myRng = Cells.FindNext(Target)
    Loop While myRng.Address <> firstCell.Address
    End If
    If Not Target Is Nothing Then
    Target.Select
    End If
    '〇の部分黄色反転致します。もうすこしなんだと思います。ヒントくれませんでしょうか

    No.1の回答に寄せられた補足コメントです。 補足日時:2020/09/08 12:28
  • うーん・・・

    Set myRng = Range("B1", Cells(Rows.Count, "B").End(xlUp))
    がぬけてました。
    しかしながら最後のとろだけ選択されます。

      補足日時:2020/09/08 12:33
  • うーん・・・

    Union(Target, c).Selectでもおなじです。

      補足日時:2020/09/08 12:34

A 回答 (3件)

補足のコードで気になる点では『変数の強制宣言(だったかな?)』にチェックを入れていないのでは?


宣言されていない変数を使っても動いてはいるみたいですし。

あとは記憶頼りのテキストエディタでの作成なので、綴りミスや未検証についてはごめんなさい。

sub try()
Dim myRng As Range, c As Range, Target As Range, myStr, firstAdd as string

Set myRng = Range("B1", Cells(Rows.Count, "B").End(xlUp))

For Each myStr In Array("ABC", "AAC")

Set c = myRng.Find(What:=myStr, LookAt:=xlPart, After:=myRng.Cells(myRng.Count))

if not c is nothing then
firstAdd = c.Address

do

if target is nothing then
set target = c
else
set target = union(target , c)
end if

set c = myrng.findnext(after:=c)

loop until c.Address = firstAdd

end if

next

if not target is nothing then target.select

end sub
    • good
    • 0
この回答へのお礼

お忙しいところありがとうございました

お礼日時:2020/09/08 20:46

何か補足が並んでいるようですが、補足については気づかない可能性が高いので遅れる場合はありますよ。



で、質問文においてですけどまず1つの語句を検索した際に
・1つしか見つからない⇒基本は質問のコードで構わない(修正と言うか勘違いがあるようですが)。
・2つ以上ヒットする⇒FindNext を使う必要あり。

まず前者において質問文のコードのミスは

Set c = myRng.Find(What:=myStr, LookAt:=xlPart, After:=myRng.Cells(myRng.Count))

If Not c Is Nothing Then
Set Target = c
Else

ここでしょう。

https://www.moug.net/tech/exvba/0050116.html

・LookAt:=xlPart
部分一致であってますか?

・If Not c Is Nothing Then
もし検索した語句がヒットしたら Set Target = c により変数:Target は書き換えられます(1つのセルに)。
For Each により複数の語句を順次検索させてもその限りです。

If Not c Is Nothing Then
 if target is nothing then
  Set Target = c
 Else
  Set Target = Union(Target, c)
 end if
End If

でしょうかね?
検索して見つかった場合とヒットしたセルオブジェクトの情報を持っていないかと言う2つの条件判断が必要かと。
    • good
    • 0
この回答へのお礼

ありがとうございます

お礼日時:2020/09/08 13:57

取り敢えず FindNextメソッドを知る事でしょうか?(コード化できない初級者より)

この回答への補足あり
    • good
    • 0
この回答へのお礼

ありがとうございます

お礼日時:2020/09/08 20:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!