2015年02月06日
オートフィルターで可視セルを扱う・・・もうひとつの方法
可視セルを対象とした繰り返し処理は次のようにも記述できます。
For Each c In Worksheets("個人データ").Columns(1).SpecialCells(xlCellTypeVisible)
↓
For Each c In Worksheets("個人データ").Range("A1").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
この記述に変えると何が違うかというと、前回ポイントにあげた
下の記述がいらなくなります。
If c = "" Then
Exit For
End If
つまり可視セルで表示されたデータの入っているセルのみ対象とできます。
For Each c In Worksheets("個人データ").Range("A1").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
If c.Row > 1 Then
Sheets("家族構成").Range("g5") = Sheets("個人データ").Range("f" & c.Row)
Sheets("家族構成").Range("o5") = Sheets("個人データ").Range("g" & c.Row)
Sheets("家族構成").Range("g6") = Sheets("個人データ").Range("h" & c.Row)
Sheets("家族構成").Range("j6") = Sheets("個人データ").Range("i" & c.Row)
Sheets("家族構成").Range("n6") = Sheets("個人データ").Range("j" & c.Row)
Sheets("家族構成").Range("e" & r) = Sheets("個人データ").Range("d" & c.Row)
Sheets("家族構成").Range("g" & r) = Sheets("個人データ").Range("a" & c.Row)
Sheets("家族構成").Range("k" & r) = Sheets("個人データ").Range("b" & c.Row)
If Sheets("個人データ").Range("b" & c.Row) <> "" Then
生年月日 = Sheets("個人データ").Range("b" & c.Row)
本日年月日 = Date
年齢 = DateDiff("yyyy", 生年月日, 本日年月日) _
+ (Format(生年月日, "mmdd") > Format(本日年月日, "mmdd"))
End If
Sheets("家族構成").Range("q" & r) = 年齢
Sheets("家族構成").Range("s" & r) = Sheets("個人データ").Range("c" & c.Row)
Sheets("家族構成").Range("u" & r) = Sheets("個人データ").Range("k" & c.Row)
r = r + 1
End If
Next c
奥が深いですが、自分で使うマクロですから、
自分がわかりやすい方法を選択していいのではないでしょうか。
For Each c In Worksheets("個人データ").Columns(1).SpecialCells(xlCellTypeVisible)
↓
For Each c In Worksheets("個人データ").Range("A1").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
この記述に変えると何が違うかというと、前回ポイントにあげた
下の記述がいらなくなります。
If c = "" Then
Exit For
End If
つまり可視セルで表示されたデータの入っているセルのみ対象とできます。
For Each c In Worksheets("個人データ").Range("A1").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
If c.Row > 1 Then
Sheets("家族構成").Range("g5") = Sheets("個人データ").Range("f" & c.Row)
Sheets("家族構成").Range("o5") = Sheets("個人データ").Range("g" & c.Row)
Sheets("家族構成").Range("g6") = Sheets("個人データ").Range("h" & c.Row)
Sheets("家族構成").Range("j6") = Sheets("個人データ").Range("i" & c.Row)
Sheets("家族構成").Range("n6") = Sheets("個人データ").Range("j" & c.Row)
Sheets("家族構成").Range("e" & r) = Sheets("個人データ").Range("d" & c.Row)
Sheets("家族構成").Range("g" & r) = Sheets("個人データ").Range("a" & c.Row)
Sheets("家族構成").Range("k" & r) = Sheets("個人データ").Range("b" & c.Row)
If Sheets("個人データ").Range("b" & c.Row) <> "" Then
生年月日 = Sheets("個人データ").Range("b" & c.Row)
本日年月日 = Date
年齢 = DateDiff("yyyy", 生年月日, 本日年月日) _
+ (Format(生年月日, "mmdd") > Format(本日年月日, "mmdd"))
End If
Sheets("家族構成").Range("q" & r) = 年齢
Sheets("家族構成").Range("s" & r) = Sheets("個人データ").Range("c" & c.Row)
Sheets("家族構成").Range("u" & r) = Sheets("個人データ").Range("k" & c.Row)
r = r + 1
End If
Next c
奥が深いですが、自分で使うマクロですから、
自分がわかりやすい方法を選択していいのではないでしょうか。
Posted by ミール at 22:04│Comments(0)
│VBA