プロが教える店舗&オフィスのセキュリティ対策術

検索して該当者をメッセージに表示するように作成してますがつまずきました。
今回行いたいのはE列を検索してB列の内容をメッセージボックスに表示します。

  Dim com As Range
Set com = Worksheets("シート1").Range("e:e").Find(what:=”野菜”, lookat:=xlWhole)

  If Not com Is Nothing Then
Dim first As Range
Set first = com
Dim compp As String
compp = com.Value

Do
Set com = Worksheets("シート1").Range("e:e").FindNext(com)
If com.Address = first.Address Then
Exit Do
End If
comm = comm & vbCrLf & com.Value
Loop

MsgBox comm

Else

End If

 この内容で実行すると野菜だけが表示されて野菜の種類が表示されません。
 どうぞアドバイスお願いします。

「vba findnext で検索し一致し」の質問画像

A 回答 (1件)

こんばんは


>B列の内容を
ならば、com.Offset(, -3).Value で B列の対象セルかな

compp = com.Value と comm = comm & vbCrLf & com.Value
MsgBox comm ?

途中にコードがあるのかな、、
お節介をすると こんな感じでは

Dim com As Range
Set com = Worksheets("シート1").Range("e:e").Find(what:="野菜", lookat:=xlWhole)

If Not com Is Nothing Then
Dim first As Range
Set first = com
Dim compp As String
compp = "<" & com.Value & ">" & vbCrLf & com.Offset(, -3).Value
Do
Set com = Worksheets("シート1").Range("e:e").FindNext(com)
If com.Address = first.Address Then
Exit Do
End If
compp = compp & vbCrLf & com.Offset(, -3).Value
Loop
MsgBox compp
Else

End If
    • good
    • 0
この回答へのお礼

こんばんは。
迅速な回答ありがとうございました。
おかげさまで無事作成することができました。

お礼日時:2021/09/17 23:54

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