なんだかGoodVibes

日々の勉強メモです。

【VBA】シート内の画像の左トップセルを取得する

今回はシート内の画像の左トップセルを取得するサンプルです。 では、サンプルです。

Sub Main()

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(1)

    Dim sh As Shape
    Dim shCount As Long: shCount = ws.Shapes.count

    Dim shTopArr() As Long
    ReDim shTopArr(1, ws.Shapes.count - 1)

    Dim idx As Long: idx = 0
    For Each sh In ws.Shapes
        shTopArr(0, idx) = sh.TopLeftCell.Row
        shTopArr(1, idx) = sh.TopLeftCell.Column
        idx = idx + 1
    Next sh

    ' 出力してみる
    For idx = 0 To UBound(shTopArr, 2)
        Dim r As Long: r = shTopArr(0, idx)
        Dim c As Long: c = shTopArr(1, idx)
        Debug.Print ("(" & r & ", " & c & ")")
    Next idx

End Sub

今回は、2次元配列に行と列を格納しています。
出力している処理のようにループを使用することで
取り出すことができます。
あとは、番号で取得することができるので
ソートするなど、使用する用途に合わせて調整したらいいかと思います。
以上です。