0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UsedRangeで選択した範囲の行数・列数を取得する

Posted at

まずは全体像。

Sub GetUsedRangeSize()
    Dim ws As Worksheet
    Dim usedRng As Range
    Dim rowCount As Long
    Dim colCount As Long
    
    ' 対象のシートを設定(必要に応じて変更)
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' 使用されている範囲を取得
    Set usedRng = ws.UsedRange
    
    ' 使用されている行数と列数を取得
    rowCount = usedRng.Rows.Count
    colCount = usedRng.Columns.Count
    
    ' 結果を表示
    MsgBox "使用されている行数: " & rowCount & vbNewLine & "使用されている列数: " & colCount, vbInformation
End Sub

1. ws.UsedRange でシート内の使用されている範囲を取得。

UsedRange の開始セルは必ず"A1"になるため、行や列の位置が途中から始まる場合でも範囲全体の行数・列数が取得できる。

2. Rows.Count で使用されている行数を取得。

3. Columns.Count で使用されている列数を取得。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?