[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『特定文字を含む行削除のマクロ』(taro)
特定文字を含む行削除のマクロという過去の質問に、このような
マクロがあるのですが、(少し変えてあります)
これですとセルの文字列が"サンプルA"だと行削除が適用されるのですが、
これを、
"サンプルA"という文字列を含む行を削除適用できるようにしたいのです。
どなたかご教授お願いします。
Sub 行削除2()
Dim i As Long Dim j As Long Dim sh As Worksheet Dim lRow As Long
For i = 1 To Worksheets.Count With Worksheets(i) lRow = .Cells(Rows.Count, 1).End(xlUp).Row
For j = lRow To 1 Step -1 If .Cells(j, 1) = "サンプルA" Then .Cells(j, 1).EntireRow.Delete ElseIf .Cells(j, 1) = "サンプルB" Then .Cells(j, 10).EntireRow.Delete ElseIf .Cells(j, 1) = "サンプルC" Then .Cells(j, 1).EntireRow.Delete End If Next j End With Next i
End Sub
< 使用 Excel:Excel2013、使用 OS:Windows7 >
>.Cells(j, 1) = "サンプルA" ここを .Cells(j, 1) Like "*サンプルA*" でどうですか? (稲葉) 2015/02/23(月) 16:39
投稿する前に同じようなもので試したんですがどこかエラーがあったみたいですね。
ありがとうございました。
(taro) 2015/02/23(月) 16:48
今は3つですけど、もしもっとたくさん消したいものがあるんだーということであれば こんなやり方もあります。 Sub 行削除3() Dim r As Long '行番号 Dim rng As Range '評価対象のセルを一時的に入れる Dim str1 As String '評価される文字列を入れる Dim str2 As Variant '評価する文字列を入れる器 Dim sh As Worksheet '対象のシート
For Each sh In Sheets With sh For r = .Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1 Set rng = .Cells(r, "A") str1 = rng '.Value For Each str2 In Array("サンプルA", "サンプルB", "サンプルC") If InStr(1, str1, str2, vbBinaryCompare) > 0 Then rng.EntireRow.Delete xlUp Exit For End If Next str2 Next r End With Next sh End Sub (稲葉) 2015/02/23(月) 17:35
それ本番のデータなのかな? If .Cells(j, 1) Like "*サンプル[A-C]*" Then で行けると思うけど? (seiya) 2015/02/23(月) 18:18
seiyaさん
本番です、出来ました。
ありがとうございます。
(taro) 2015/02/24(火) 08:57
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.