• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
langQ
博客园    首页    新随笔    联系   管理    订阅  订阅
【VBA】可視セルや空白セルの位置を取得【SpecialCellsを使う】

参考元:【VBA】可視セルや空白セルの位置を取得【SpecialCellsを使う】
https://daitaideit.com/vba-specialcells/

SpecialCellsの使い方

'コメント
Cells.SpecialCells(xlCellTypeComments).Select
'定数
Cells.SpecialCells(xlCellTypeConstants).Select 'すべて
Cells.SpecialCells(xlCellTypeConstants, 1).Select '数値
Cells.SpecialCells(xlCellTypeConstants, 2).Select '文字
'数式
Cells.SpecialCells(xlCellTypeFormulas).Select 'すべて
Cells.SpecialCells(xlCellTypeFormulas, 1).Select '数値
Cells.SpecialCells(xlCellTypeFormulas, 2).Select '文字
Cells.SpecialCells(xlCellTypeFormulas, 16).Select 'エラー
'空白セル
Cells.SpecialCells(xlCellTypeBlanks).Select
'最後のセル
Cells.SpecialCells(xlCellTypeLastCell).Select
'可視セル
Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Select
'条件付き書式
Cells.SpecialCells(xlCellTypeAllFormatConditions).Select 'すべて
Range("A1").SpecialCells(xlCellTypeSameFormatConditions).Select '同じ条件
'データの入力規則
Cells.SpecialCells(xlCellTypeAllValidation).Select 'すべて
Range("A1").SpecialCells(xlCellTypeSameValidation).Select '同じ規則

例:コメントのセルを選択

Sub TEST1()
    
    'コメントのセルを選択
    Cells.SpecialCells(xlCellTypeComments).Select
    
End Sub

実行結果:


 

■可視セルを選択して不要行を削除

▼可視セルのみを削除
使う表

Sub TEST16()
    
    'A列で「A」以外である行を非表示
    For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
        If Cells(i, "A") <> "A" Then
            Rows(i).Hidden = True '非表示
        End If
    Next
    
    '表全体の値のみを取得
    With Range("A1").CurrentRegion.Offset(1, 0)
        .Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Delete
    End With
    
    'すべての行を表示
    Rows("1:100").Hidden = False
    
End Sub
手順としては、

①A列が『A』以外である行を非表示にする
②表全体の値のみを取得する
③可視セルを削除する
④行をすべて表示する

実行結果:

▼オートフィルタで表示行のみを削除

Sub TEST17()
    
    'A列を、「A」でフィルタ
    Range("A1").AutoFilter 1, "A"
    
    'フィルタした行を削除
    With Range("A1").CurrentRegion.Offset(1, 0)
        .Resize(.Rows.Count - 1).EntireRow.Delete
    End With
    
    'フィルタを解除
    Range("A1").AutoFilter
    
End Sub
VBAコードの流れとしては、

①オートフィルタでA列を『"A"』でフィルタ
②表示している行を削除
③フィルタを解除
という感じです。

実行結果:

posted on 2024-10-01 23:43  嚯嚯go  阅读(227)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3