• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:エクセルVBAで、文字列の検索方法について)

エクセルVBAで文字列の検索方法について

freedniaの回答

  • ベストアンサー
  • freednia
  • ベストアンサー率25% (84/324)
回答No.1

お疲れ様です。 If instr(Cells(x, 5), "営業") <> 0 Then でいいと思いますよ。

joker999
質問者

お礼

InStr関数というのがあるんですね! 初めて知りました。 VBAってすごいですねえ。

関連するQ&A

  • エクセル2002のVBAで太文字を検索したいのですが、

    エクセル2002のVBAで太文字を検索したいのですが、 <状況> B列にチェックしたい文字が入力されています <やりたいこと> B列に入力されている文字の中から、太文字のみを抜き出して、 太字の見つかった行のG列に太文字のみを抜き出して複写したい <自作マクロの現状> セル全体の太文字検索は下記のマクロ「太字検索チェック1」で完成しましたが、 セルに記載してある文字の中で「一部は普通文字、のこり一部は太文字」と混在 しているセルの中身から太文字部分のみを別のセルに抜き出したいのですが、 そのマクロを「太字検索チェック2」のように書きました。 しかし「太字検索チェック2」の「 If dat.Font.Bold = True Then」の部分で 「型が違う・・・」のエラーで先に進みません。 どなたか、セルの中身の太文字のみを抜き出すマクロを教えてください よろしくお願いします   Sub 太字検索チェック1() i = 3 Worksheets("テスト").Activate For Each myRng In Range("B:B") セル = "b" & i If Range(セル).Font.Bold = True Then Cells(i, 7) = Cells(i, 2) ’太字のCells(i, 2)を Cells(i, 7) にコピー End If i = i + 1 If i = 1703 Then ’1703番地で終了 Exit For End If Next End Sub Sub 太字検索チェック2() i = 3 Worksheets("テスト").Activate For Each myRng In Range("B:B") 内容 = Cells(i, 2) 文字数 = Len(内容) For p = 1 To 文字数 dat = Mid(内容, p, 1)       X=8+P If dat.Font.Bold = True Then  ’1文字づつ太字を検索 Cells(i, X) = dat        ’太字なら→Cells(i, X) にコピー End If Next i = i + 1 If i = 1703 Then Exit For End If Next End Sub

  • エクセル マクロ 複数セルの色付けについて

    マクロ初心者で苦戦しており、教えていただけると大変ありがたいです。 エクセルの複数のシートに、問2_1 、問2_2 、問2_10 (全部で問は2_28まであります)等の文字列があり、その文字に合わせて、そのセルだけ色を付けたいと考えております。 今書いているコードだと、ワークシートのA列にある場合は色が付けられるのですが、それ以外に色が付けられません。どこが間違っているでしょうか? どうぞご指導お願いいたします。 Sub 色つけ() Dim Maxrow As Long Dim i As Long Maxrow = Cells(Rows.Count, i).End(xlUp).Row For i = 1 To Maxrow If InStr(Cells(i, 1), "2_1 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 6 If InStr(Cells(i, 1), "2_2 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 6 If InStr(Cells(i, 1), "2_23 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 23 If InStr(Cells(i, 1), "2_13 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 4 If InStr(Cells(i, 1), "2_6 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 7 Next i End Sub

  • excel VBAの文字列設定

    VBAで繰り返し作業を行っているのですが変数と文字列の組み合わせの設定に困っています。 for ~next(変数x)で繰り返しを行い medx の文字列を検索したいのですがうまくいきません。 疑問の部分は以下の範囲です 定義 x, y, i, ro, li as integer med1~medx までそれぞれ別の文字列(ex. med1= a med2 = b・・・ 統一性はないです) VBA部分 If y > 1 Then For x = 1 To y - 1 For i = 1 To 13 ←sheet数 For ro = 1 To 150 ←行 For li = 1 To 200 ←列 Worksheets("sheet" & i).Select If InStr(Cells(ro, li), "med" & x) > 0 Then ←疑問部分 med1、med2・・・を含むかどうか Cells(ro, li).Interior.ColorIndex = 3 Else End If Next Next Next Next else end if 上のように書くと"med1"を含むかどうがになってしまい、"a"を含むかどうかになりません。 わかりにくいかもしれませんが回答どうぞお願いします。

  • Excel VBAで検索(Win2000 Excel2000)

    現在、下記のようなコードを書いています。データテーブルの縦と横の検索値を探してその列数と行数を返したいのですが、下記の Sub検索1 と Sub検索2 を1つのSubで実行させるにはどうしたらよいのでしょうか?よろしくお願い致します。 ----------------------------------------- Sub 検索1() Worksheets("Data").Activate Dim x As Integer For x = 3 To 22 If Cells(2, x).Value >= 12 Then MsgBox x Exit Sub End If Next MsgBox "見つかりません" End Sub --------------------------------------------- Sub 検索2() Worksheets("Data").Activate Dim i As Integer For i = 4 To 42 If Cells(i, 2).Value = "A" Then MsgBox i Exit Sub End If Next MsgBox "見つかりません" End Sub

  • vba検索結果を保持しつつ、次の検索結果が欲しい

    a列にあるセルがe列にないか検索し、あった場合は、b列にあるセルがf列にないか検索し、あった場合は、c列にあるセルがg列にないか検索し、あった場合は、c列とg列が合致した2つ隣のセル(i列)に、d列にあるセルとh列にあるセルを結合させた結果を、表示させたいです。 以下のコードを走らせましたが、何も起こりませんてした。 お手数ですが、ご教示いただけますと幸いですm(_ _)m sub merge () dim i as long for i = 1 to cells(rows.count,1).end(xlup).row if cells(i,1) = cells(i,5) then if cells(i,2) = cells(i,6) then if cells(i,3) = cells(i,7) then cells(i,7).offset(0,2) = cells(i,4) and cecls(i,8) i = i + 1 end if end if end if next end sub

  • エクセルで選択中の列や行を見やすくしたい

    タイトルのとおり、選択中の列や行の色が一列全部変るように したいと思い調べ、以下のVBEコードを見つけたんですが Public m Private Sub Worksheet_SelectionChange(ByVal Target As Range)  If m <> 0 Then   Range(Cells(m, 1), Cells(m, 256)).Interior.ColorIndex = xlNone  End If  Range(Cells(Target.Row, 1), Cells(Target.Row, 256)).Interior.ColorIndex = 6  m = Target.Row End Sub 確かに色は変るんですが、もともとついている箇所の色が 消えていってしまいます。 色が消えずに同じことは出来ないでしょうか。 ご存知の方いらっしゃいましたら教えてください。 よろしくお願いします。

  • Excel VBAで文字列の部分一致の文字列を表示

    以前、こちらで頭5文字までの一致で文字列を表示するVBAを教えて頂きました。今回はFINDなどの部分一致での文字列を表示することをしたいのですが、ご教示いただけますと幸いです。 下記はSheet1のA3に文字を5文字以内いれるとSheet2のC列からピックアップしてSheet1のA列に文字列を表示する及びSheet2のB列のデータをSheet1のC列に表示させるVBAです。 Sub Test2() Dim Ws1 As Worksheet, Ws2 As Worksheet Dim SData As String, i As Long, VRet As Variant Dim c As Range, LastRow As Long Set Ws1 = Sheets("Sheet1") Set Ws2 = Sheets("Sheet2") SData = CStr(Ws1.Range("A3").Value) If SData = "" Then Exit Sub i = 5 LastRow = Ws1.Cells(Rows.Count, "A").End(xlUp).Row If LastRow < i Then LastRow = i End If Ws1.Range(Ws1.Cells(i, "A"), Ws1.Cells(LastRow, "C")).ClearContents With Ws2 For Each c In .Range(.Cells(1, "C"), .Cells(Rows.Count, "C").End(xlUp)) VRet = InStr(1, CStr(c.Value), SData, vbTextCompare) If VRet = 1 And Len(c.Value) >= 10 Then Ws1.Cells(i, "A").Resize(1, 2).Value = c.Resize(1, 2).Value 'とりあえずSheet2のB列のデータ(C列のデータに同期したデータ)をSheet1のC列に Ws1.Cells(i, "C").Value = .Cells(c.Row, "B").Value i = i + 1 End If Next End With End Sub 宜しくお願い致します。

  • エクセル マクロ VBA 罫線 文字列

    職場で使う表をVBAマクロを用いて罫線作成をしています。 前任者のアレンジを頼まれたのですが前任者に連絡が取れず困っています。 表の特徴は以下のようになります。 ・A列を飛ばし、B列から2列飛びで文字を記入 ・b2=曜日、b3=1、b4=2、b5=3、b6=4、b7=空白のセットが曜日ごとに2セット×7日分 この表を ・b2=曜日、b3=-3、b4=-2、b5=-1、b6=0、b7=1、b8=2、b9=3、b10=4、b11=空白のセットが曜日ごとに2セット×7日分 に変更したいのですが空欄の場所がずれてしまい上手くいきません。 原本のマクロは以下です。 ---------------------------------------------------------------- Sub 罫線作成() Range(Cells(4, 1), Cells(86, 22)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ch1 = "月火水木金土日" For i = 4 To 76 Step 12 n1 = (i + 8) \ 12 Range(Cells(i, 1), Cells(i + 10, 22)).Select With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With 'Cells(i, 1) = Mid(ch1, n1, 1) For i2 = 2 To 20 Step 3 For i3 = i To i + 10 nb1 = (i3 + 8) Mod 12 If nb1 = 0 Or nb1 = 6 Then Cells(i3, i2) = Mid(ch1, n1, 1) If nb1 = 1 Or nb1 = 7 Then Cells(i3, i2) = 1 If nb1 = 2 Or nb1 = 8 Then Cells(i3, i2) = 2 If nb1 = 3 Or nb1 = 9 Then Cells(i3, i2) = 3 If nb1 = 4 Or nb1 = 10 Then Cells(i3, i2) = 4 Next Next Next End Sub ---------------------------------------------------------------- 4行目から142行目まで使用することは分かっているのですが… どうかご助力お願いします。

  • 文字ではなく数字で比較

    下のマクロで列を比較しますが,2358と12358が同じ文字になります. 下の文字を数字に変えたいです. Sub 比較して色を付ける() '比較部分 Dim RetRange As Range Range("D2:D35323").Select For i = 1 To 35323 Set RetRange = Selection.Find(What:=Cells(i, 4).Value, _ after:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext) If Not RetRange Is Nothing Then If RetRange.Address <> Cells(i, 4).Address Then RetRange.Interior.ColorIndex = 6 Cells(i, 4).Interior.ColorIndex = 6 End If End If Next END Sub マクロは全然知らない初心者です. よろしくお願いします.

  • VBAで教えてください。

    以前ここで教えていただいたVBAで http://jisaku.155cm.com/src/1371930716_9b9006528605642980beed48a8998013b0731e4b.jpg のようにA列のテスト4をクリックしたときにC列のテスト4が一発で解るようにしたいです。 もちろん、テスト11をクリックしたときは、テスト4塗りつぶしは解除され、 テスト11が塗りつぶされるようにしたいです。 写真は塗りつぶししていますが、解るようにしたいだけなので、塗りつぶしにはこだわっていません。 あと、E、F、G列は解りやすく並べているだけで、実際はA、B、C列だけです。 それと、C列は関数を使って表示してあります。 という質問で Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'この行から Dim i As Long Range("C:C").Interior.ColorIndex = xlNone If Application.Intersect(Target, Range("A:A")) Is Nothing Or Target.Count <> 1 Then Exit Sub On Error Resume Next Application.ScreenUpdating = False ActiveSheet.Cells.interio.ColorIndex = xlNone For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, "C") = Target Then Cells(i, "C").Interior.ColorIndex = 3 End If Next i Application.ScreenUpdating = True End Sub 'この行まで をシートのコードに張り付ければいいですよ。と教えてくれたものがあるのですが、 A列でクリックした文字をC列からすべて見つけて反転してくれないようです。何個か反転してくれない ものが出てきてしまいました。 C列が何百行とかなってしまうと、すべての同じ文字を検索してくれないのでしょうか? ちなみに列がここに掲載しているものと違うので Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'この行から Dim i As Long Range("R:R").Interior.ColorIndex = xlNone If Application.Intersect(Target, Range("B:B")) Is Nothing Or Target.Count <> 1 Then Exit Sub On Error Resume Next Application.ScreenUpdating = False ActiveSheet.Cells.interio.ColorIndex = xlNone For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, "R") = Target Then Cells(i, "R").Interior.ColorIndex = 3 End If Next i Application.ScreenUpdating = True End Sub 'この行まで のCをRにAをBに変更して使ってます。 これがいけないのかな? よろしくお願いします。