[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『複数条件での検索』(zunzun)
A B C D E F
1 幅 5≦6 5 6
2 角度 60≦70 60 70
3 形 丸型
4 高さ 7≦8 7 8
5
6 TYPE 幅 角度 形 高さ
7 ● 1 5.2 65 丸型 7
8 2 6.2 65 角型 5
9 3 8.5 80 丸型 9
7行目以下にTYPE毎(1〜1000)のデータがあります。
B1,B2,B3,B4に検索したい条件が入力しています。
D1:E4は、条件の左右の値を抜き出すようにしてあります。
値がある場合の検索するマクロ(一致したらA列に●を入力)はできたのですが、
B3以外をブランクにしても検索(例えばB1だけブランク)する方法はあるのでしょうか?
よろしくお願いします。
< 使用 Excel:Excel2010、使用 OS:Windows7 >
とりあえず書きましたが検証はしていません。
Sub Sample()
Dim c As Range
Dim chkA As Variant
Dim chkW As Variant
Dim chkH As Variant
Dim chkS As Variant
Dim minA As Variant
Dim maxA As Variant
Dim minW As Variant
Dim maxW As Variant
Dim minH As Variant
Dim maxH As Variant
Dim ng As Boolean
chkW = Range("B1").Value
minW = Range("D1").Value
maxW = Range("E1").Value
chkA = Range("B2").Value
minA = Range("D2").Value
maxA = Range("E2").Value
chkS = Range("B3").Value
chkH = Range("B4").Value
minH = Range("D4").Value
maxH = Range("E4").Value
With Range("B7", Range("B" & Rows.Count).End(xlUp))
.Offset(, -1).ClearContents
For Each c In .Cells
ng = False
With c.EntireRow
If Not IsEmpty(chkW) Then
If .Range("C1").Value < minW Or .Range("C1").Value > maxW Then ng = True
End If
If Not IsEmpty(chkA) Then
If .Range("D1").Value < minA Or .Range("D1").Value > maxA Then ng = True
End If
If Not IsEmpty(chkS) Then
If .Range("E1").Value <> chkS Then ng = True
End If
If Not IsEmpty(chkH) Then
If .Range("F1").Value < minH Or .Range("F1").Value > maxH Then ng = True
End If
If Not ng Then .Range("A1").Value = "●"
End With
Next
End With
End Sub
(β) 2015/11/26(木) 18:48
書き込もうとしたら解決済みになっていたが、せっかく作ったので。
関数だが。
A7セルに =IF(AND(OR(B$1="",AND(C7>=D$1,C7<=E$1)),OR(B$2="",AND(D7>=D$2,D7<=E$2)),B$3=E7,OR(B$4="",AND(F7>=D$4,F7<=E$4))),"●","") と入力して下へフィルコピーではどうか。 (ねむねむ) 2015/11/27(金) 09:14
別案 条件範囲のレイアウトを下記に変更
A B C D E F G 幅 幅 角度 角度 形 高さ 高さ >=5 <=6 >=60 <=70 丸型 >=7 <=8
Sub test()
Range("b6").CurrentRegion.AdvancedFilter 1, [a1:g2]
End Sub
(seiya) 2015/11/27(金) 10:04
念のため形部分の条件は
形 =丸型
のほうがいいかと。
比較演算子をつけない場合は前方一致になるため、たとえば「丸型」「丸型A」などがあった場合のため。 (ねむねむ) 2015/11/27(金) 10:37
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.